Str/preg_Replacing An Array With The Same Words?

Okay so I want to replace 2 words with the same words in a different format but only once per occurance.

[php]<?php
$text = “Word 1”;
$newtext = str_replace(array(‘Word 1’,‘Word 2’),‘Word 1,Word 2’,$text);
echo $newtext . “
\n”;
?>[/php]

This outputs:
Word 1,Word 1,Word 2

I want it to instead out put like this:
Word 1,Word2

That’s because the replacement takes place left to right. So in your initial string “Word1” is replaced by “Word1,Word2”. Your string now contains “Word2”, which is then searched for and replaced by “Word1,Word2”, giving the result you say.

Try:

[php]$text = “Word 1”;
$newtext = str_replace(array(‘Word 1’,‘Word 2’,’’,’|’),array(’’,’|’,‘Word 1,Word 2’,‘Word 1,Word 2’),$text);
echo $newtext . “
\n”;[/php]

thanks for the reply, I actually got helped on another forum. Thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service