0

我有一个字符串abcxdefxghix。我希望删除除第一个之外的所有“x”。我可以使用 轻松找到第一个“x”的位置strpos(),因此希望删除该位置之后的所有“x”。 str_replace()用另一个替换给定的字符串,但不允许开始位置。 substr_replace()给出一个起始位置,但没有搜索参数。我意识到这可以使用来完成,preg_replace()但似乎没有正则表达式(或没有一些疯狂的拆分/替换/组装策略)也应该是可能的。

4

3 回答 3

1

你可以这样做:

list($first,$remainder) = explode($searchString,$subjectString,2);
$remainder = str_replace($searchString,$replacementString,$remainder);
$resultString = $first.$searchString.$remainder;
于 2013-10-06T14:27:21.093 回答
0

我很可能会用老式的方式来做:

$index = strpos($input, $needle);
if ($index !== false) {
    $input = substr($input, 0, $index + 1).
             str_replace($needle, $replacement, substr($input, $index + 1));
}
于 2013-10-06T14:31:27.000 回答
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
于 2013-10-06T15:10:39.307 回答