Mark Baker wrote:str_replace(' X ',' new text ',$string);
That won't work. It replaces all X's with new text. Not the first occurence.
EDIT:
Here's the deal.
Blah X Blah X Blah X
Code: Select all
$x1 = $_GET...
$x2 = ...
$x3 = ...
Now replace first X with $x1, then second X with $x2, ... but basic str_replace replaces ALL x's, not first /second. So I made this function:
Code: Select all
public static function str_replace_count($search,$replace,$subject,$times) {
$subject_original=$subject;
$len=strlen($search);
$pos=0;
for ($i=1;$i<=$times;$i++) {
$pos=strpos($subject,$search,$pos);
if($pos!==false) {
$subject=substr($subject_original,0,$pos);
$subject.=$replace;
$subject.=substr($subject_original,$pos+$len);
$subject_original=$subject;
} else {
break;
}
}
return($subject);
}
But, if $x1 equals to lets say "hey mrx!" then it becomes
Blah hey mrx X blah X
And now the second X replacement will replace the mrx not the individual X.
Does anyone understand?