note 40060 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jun 2006 13:18:19 +0000 |
| Subject: | note 40060 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114325@lists.php.net to get a copy of this message | ||
Note Submitter: Massimo
----
I've made a little modification to the code written by daniil for replacing variables inside
brackets with values because if there is there isn't spaces between tags that we want to
replace it doesn't work. Using '\w' instead of \S solve this problem.
Example:
<?php
// This is taken from somewhere and written in PHP
$str = 'Hello, [name]
This is my phone number [phone]-[number]';
// Those are the variables to be replaced with inside []
$name = 'Mike';
$phone = '+39';
$number = '555...';
$msg = preg_replace("/\[(\w+)\]/e", "\$\\1", $str);
print $msg;
?>
The above script will produce the desired output replacing variables inside brackets with values in
the php script.