View Full Version : Simple string replacement

06-11-2007, 08:51 PM
Hello. If I have the following array...

$parameters = array(
'store="{STORE}"' => '$tags[\'stores\'][{STORE}]',
'for="{QUESTION}"' => 'box_send("{QUESTION}");'

I would like that from a string, PHP could replace it. For example...

$string = 'My string and for="MY STUFF" wohooo!'

Is there anyway that from that array PHP would turn this string into:

$resulting_string = 'My string and box_send("MY STUFF");';

I have been googling and php.net'ing for two days now, and after many attempts and observation of other scripts, I couldn't make it. Can you help me? Thanks in advance!

mr e
06-11-2007, 09:16 PM
What you want is regular expressions and preg_replace (http://www.php.net/preg_replace)

Something like this, if you would like it explained just let me know

$string = 'My string and for="MY STUFF" wohooo!';

// $result = My string and box_send("MY STUFF") wohooo!
$result = preg_replace('/\sfor="(.*)"\s/i', ' box_send("$1") ', $string);

06-11-2007, 10:22 PM
Well, yeah. I just needed an explanation becaue I don't quite understand the Perl regular expressions.

mr e
06-11-2007, 11:36 PM
Haha no problem, they definitely have a learning curve, and not an easy one at that

preg_replace('/\sfor="(.*)"\s/i', ' box_send("$1") ', $string);

First their are the two /'s, the regex goes between these, so if you were just looking to replace the word "for", your regex would be "/for/"

Then after the last / there is the "i" modifier, which simple means the search is case insensitive

So we're searching for a space (represented by \s, which means any kind of space, e.g. a space, tab, newline etc)

Followed by the string for="

**Here's our matching pattern inside the ()'s**
Followed by any character (represented by the period) zero or more times (represented by the *)
(Zero or more means the string could be empty, as opposed to the + which is one or more, and the ? which is zero or one)

Followed by the string ", then another space

Then in our replace, we have the string we want to replace it with, box_send("$1"), where the $1 means the first sub pattern (the pattern inside the ()'s matched, $2 would be the second if we had one, etc)

Hope this helps some, I know how hard regex's were when I first started playing with them, not the most friendly, but definitely cool

06-12-2007, 12:05 AM
Oh... So I could do:

$parameters = array(

'store' => '$tags[\'stores\'][$1]',
'for' => 'box_send("$1");'

foreach($parameters as $parameter => $replacement) {
preg_replace('/\s'.$parameter.'="(.*)"\s/i', $replacement, $string);

Is this correct? I changed the keys and values in the array accordingly. Please tell me if this is correct.

mr e
06-12-2007, 12:31 AM
You can actually just give preg_replace two arrays and it'll do it for you

The only thing I saw wrong was I believe you needed to escape the $ in $tags

And on second thought, you might not want the last \s otherwise it won't match for="something". because of that period (or comma, whatever)

I also added the U modifier to make the pattern ungreedy, as it was, if you had more than one " it would match the first, then the very last (aka greedy), instead of the next

// $string = 'This is a store="test", and for="fun"!';

$find = array('/\sstore="(.*)"/iU', '/\sfor="(.*)"/iU');
$replace = array(' $tags[\'stores\'][$1]', ' box_send("$1");');

$result = preg_replace($find, $replace, $string);

06-12-2007, 06:22 PM
Gee, thanks, mr e. :) You've been a great help ;)