我有一个字符串abcxdefxghix
。我希望删除除第一个之外的所有“x”。我可以使用strpos()
轻松找到第一个“x”的位置,因此希望删除该位置后的所有“x”。 str_replace()
执行替换给定字符串与另一个字符串,但不允许起始位置。 substr_replace()
给出了一个起始位置,但没有搜索参数。我意识到这可以使用preg_replace()
完成,但似乎没有正则表达式(或者没有一些疯狂的拆分/替换/汇编策略)也应该可以。
答案 0 :(得分:1)
你可以这样做:
list($first,$remainder) = explode($searchString,$subjectString,2);
$remainder = str_replace($searchString,$replacementString,$remainder);
$resultString = $first.$searchString.$remainder;
答案 1 :(得分:0)
我最有可能采用传统方式:
$index = strpos($input, $needle);
if ($index !== false) {
$input = substr($input, 0, $index + 1).
str_replace($needle, $replacement, substr($input, $index + 1));
}
答案 2 :(得分:0)
我认为必须有一种更简单的方法,但显然没有。我的朴素功能被忽略了,但如果有一种“旧时尚方式”,那可能就是这样。
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