I have a string abcxdefxghix
. I wish to remove all "x"s except the first one. I can easily find the position of the first "x" using strpos()
, so wish to remove all "x"s after that position. str_replace()
performs a replacement of a given string with another, but doesn't allow a start position. substr_replace()
gives a start position, but doesn't have the search parameter. I realize this can be done using preg_replace()
but it seems like it should also be possible with without regular expressions (or without some crazy split/replace/assemble strategy).
I 'd most likely do it the old-fashioned way:
$index = strpos($input, $needle);
if ($index !== false) {
$input = substr($input, 0, $index + 1).
str_replace($needle, $replacement, substr($input, $index + 1));
}
You could do something like this:
list($first,$remainder) = explode($searchString,$subjectString,2);
$remainder = str_replace($searchString,$replacementString,$remainder);
$resultString = $first.$searchString.$remainder;
I thought there had to be an easier way, but evidently there isn't. My homespun function was negligently faster, but if there is an "old-fashion way", it is probably the way to go.
function replace_all_but_first_1($search,$replace,$subject){
$pos=strpos($subject,$search);
return ($pos===false)?$subject:substr($subject,0,$pos+1).str_replace($search,$replace,substr($subject, $pos));
}
function replace_all_but_first_2($search,$replace,$subject){
$index = strpos($subject, $search);
if ($index !== false) {
$subject = substr($subject, 0, $index + 1).
str_replace($search, $replace, substr($subject, $index + 1));
}
return $subject;
}
function replace_all_but_first_3($search,$replace,$subject){
list($first,$remainder) = explode($search,$subject,2);
$remainder = str_replace($search,$replace,$remainder);
$resultString = $first.$search.$remainder;
return $resultString;
}
function replace_all_but_first($search,$replace,$subject){
echo('Replace "'.$search.'" with "'.$replace.'" in "'.$subject.'"<br><br>');
echo('replace_all_but_first_1: '.replace_all_but_first_1($search,$replace,$subject)."<br>");
echo('replace_all_but_first_2: '.replace_all_but_first_2($search,$replace,$subject)."<br>");
echo('replace_all_but_first_3: '.replace_all_but_first_3($search,$replace,$subject)."<br>");
$time=microtime(true);
for ($i = 1; $i <= 100000; $i++) {$x=replace_all_but_first_1($search,$replace,$subject);}
echo('replace_all_but_first_1 '.(microtime(true)-$time).'<br>');
$time=microtime(true);
for ($i = 1; $i <= 100000; $i++) {$x=replace_all_but_first_2($search,$replace,$subject);}
echo('replace_all_but_first_2 '.(microtime(true)-$time).'<br>');
$time=microtime(true);
for ($i = 1; $i <= 100000; $i++) {$x=replace_all_but_first_3($search,$replace,$subject);}
echo('replace_all_but_first_3 '.(microtime(true)-$time).'<br>');
echo('<br><br><br>');
}
replace_all_but_first('x','','abcxdefxghix');
replace_all_but_first('x','','xabcxdefxghix');
replace_all_but_first('z','','abcxdefxghix');
Replace "x" with "" in "abcxdefxghix"
replace_all_but_first_1: abcxdefghi
replace_all_but_first_2: abcxdefghi
replace_all_but_first_3: abcxdefghi
replace_all_but_first_1 0.18973803520203
replace_all_but_first_2 0.19031405448914
replace_all_but_first_3 0.19151902198792
Replace "x" with "" in "xabcxdefxghix"
replace_all_but_first_1: xabcdefghi
replace_all_but_first_2: xabcdefghi
replace_all_but_first_3: xabcdefghi
replace_all_but_first_1 0.18725895881653
replace_all_but_first_2 0.19358086585999
replace_all_but_first_3 0.19228482246399
Replace "z" with "" in "abcxdefxghix"
replace_all_but_first_1: abcxdefxghix
replace_all_but_first_2: abcxdefxghix
replace_all_but_first_3: abcxdefxghixz
replace_all_but_first_1 0.074465036392212
replace_all_but_first_2 0.075581073760986
replace_all_but_first_3 0.71253705024719