note 40060 added to function.preg-replace
| From: | Massimo at rn2 dot php dot net | Date: | Fri, 20 Feb 2004 12:46:37 +0000 |
| Subject: | note 40060 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-65443@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40060
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40060&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40060&report=yes
Search -- http://master.php.net/manage/user-notes.php