note 38502 added to function.preg-replace

From: Date: Wed, 24 Dec 2003 14:57:22 +0000
Subject: note 38502 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-62423@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38502 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38502&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38502&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#62423) next »