Wondering how to replace in php the only first occurrence of the string?
just copy and paste this code.
<?
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);
}?>
Wondering how to replace in php the only first occurence
of the string?
just copy and paste this code.




Comments
There are no comments for this post.
Write a Comment