preg_replace help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dncnllama
Forum Newbie
Posts: 1
Joined: Fri Mar 21, 2008 7:23 pm

preg_replace help

Post by dncnllama »

I have the following code:

Code: Select all

$string = $ctemplate[0];
$patterns[0] = '/#CLIENT_NAME#/';
$patterns[1] = '/#CLIENT_AREA#/';
$patterns[2] = '/#CLIENT_BED#/';
$patterns[3] = '/#CLIENT_BATH#/';
$patterns[4] = '/#CLIENT_AMTFROM#/';
$patterns[5] = '/#CLIENT_AMTTO#/';
$patterns[6] = '/#CLIENT_START#/';
$patterns[7] = '/#CLIENT_EMAIL#/';
$patterns[8] = '/#CLIENT_CELL#/';
$patterns[9] = '/#CLIENT_WHOTOLD#/';
$replacements[0] = $_SESSION["name"];
$replacements[1] = $newLocation;
$replacements[2] = $newBed;
$replacements[3] = $_SESSION["bath"];
$replacements[4] = $_SESSION["amt_from"]
$replacements[5] = $_SESSION["amt_to"];
$replacements[6] = $newStart;
$replacements[7] = $_SESSION["email"];
$replacements[8] = $_SESSION["cell"];
$replacements[9] = $_SESSION["who_told"];
 
ksort($patterns);
ksort($replacements);
$newmessage = preg_replace($patterns, $replacements, $string);
echo $newmessage;
It all works except for $_SESSION["amt_from"] and $_SESSION["amt_to"] are in format like $3,500 for example, but when preg_replace is done it ends up being just '500'
I have tried doing $replacements[4] = '/\'.$_SESSION["amt_from"]
and a few other variations with the slashes but can't get it to work, if I hardcode
$replacements[4] = '\$3,500'; that works fine but I need to use the session variable.
I have looked at php.net but can't find a solution
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: preg_replace help

Post by Christopher »

You can just use str_replace() for this because you are not doing any pattern matching. Just get rid of the /'s in the $patterns array. You don't need the ksort()'s either.
(#10850)
Post Reply