note 38502 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jun 2006 13:17:02 +0000 |
| Subject: | note 38502 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114301@lists.php.net to get a copy of this message | ||
Note Submitter: daniil at nycfoto dot com
----
I've had this problem where I had to take a message from the database and replace special
strings into some useful data. Since others might want to do the same, here is an example:
<?php
// This is taken from somewhere and written in PHP
$str = 'Hello, [name]
thank you for bla & bla,
Sincerely,
[team_name]';
// Those are the variables to be replaced with inside []
$name = 'Mike';
$team_name = 'Company XYZ';
$msg = preg_replace("/\[(\S+)\]/e", "\$\\1", $str);
print $msg;
?>
The above script will produce the desired output replacing variables inside brackets with values in
the php script.