使用str_replace使它只对第一个匹配起作用?

时间:2009-08-10 00:39:55

标签: php string

我想要的str_replace()版本仅替换$search中第一次出现的$subject。有一个简单的解决方案,还是我需要一个hacky解决方案?

22 个答案:

答案 0 :(得分:521)

没有它的版本,但解决方案根本不是hacky。

$pos = strpos($haystack, $needle);
if ($pos !== false) {
    $newstring = substr_replace($haystack, $replace, $pos, strlen($needle));
}

非常简单,并且可以节省正则表达式的性能损失。

红利:如果您想要替换 last ,请使用strrpos代替strpos

答案 1 :(得分:306)

可以使用preg_replace

完成
function str_replace_first($from, $to, $content)
{
    $from = '/'.preg_quote($from, '/').'/';

    return preg_replace($from, $to, $content, 1);
}

echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
// outputs '123def abcdef abcdef'

魔法在可选的第四个参数[Limit]中。来自文档:

  

[限制] - 最大可能   每个模式的替换   主题字符串。默认为-1(否   极限)。


但是,请参阅zombat's answer以获得更有效的方法(大约快3-4倍)。

答案 2 :(得分:90)

编辑:两个答案都已更新,现在正确无误。我会留下答案,因为功能时间仍然有用。

遗憾的是,“zombat”和“太多php”的答案都不正确。这是对zombat发布的答案的修订(因为我没有足够的声誉发表评论):

$pos = strpos($haystack,$needle);
if ($pos !== false) {
    $newstring = substr_replace($haystack,$replace,$pos,strlen($needle));
}

注意strlen($ needle),而不是strlen($ replace)。只有当针和替换长度相同时,Zombat的示例才能正常工作。

这是与PHP自己的str_replace具有相同签名的函数中的相同功能:

function str_replace_first($search, $replace, $subject) {
    $pos = strpos($subject, $search);
    if ($pos !== false) {
        return substr_replace($subject, $replace, $pos, strlen($search));
    }
    return $subject;
}

这是“太多PHP”的修订答案:

implode($replace, explode($search, $subject, 2));

注意结尾2而不是1.或者是函数格式:

function str_replace_first($search, $replace, $subject) {
    return implode($replace, explode($search, $subject, 2));
}

我将这两个函数计时,第一个函数在没有找到匹配时快两倍。找到匹配项时速度相同。

答案 3 :(得分:67)

我想知道哪一个是最快的,所以我对它们进行了全部测试。

您将在下面找到:

  • 此页面上提供的所有功能的完整列表
  • 每个制度的基准测试(平均执行时间超过10,000次)
  • 每个答案的链接(完整代码)

所有功能都使用相同的设置进行测试:

$string = 'OOO.OOO.OOO.S';
$search = 'OOO'; 
$replace = 'B';

仅替换字符串中第一个出现的字符串的函数:


仅替换字符串中最后出现的字符串的函数:

答案 4 :(得分:53)

不幸的是,我不知道任何可以做到这一点的PHP函数 你可以像这样轻松地自己动手:

function replace_first($find, $replace, $subject) {
    // stolen from the comments at PHP.net/str_replace
    // Splits $subject into an array of 2 items by $find,
    // and then joins the array with $replace
    return implode($replace, explode($find, $subject, 2));
}

答案 5 :(得分:7)

我创建了这个 little 函数,用字符串(区分大小写)替换字符串,不需要Regexp。它工作正常。

function str_replace_limit($search, $replace, $string, $limit = 1) {
    $pos = strpos($string, $search);

    if ($pos === false) {
        return $string;
    }

    $searchLen = strlen($search);

    for ($i = 0; $i < $limit; $i++) {
        $string = substr_replace($string, $replace, $pos, $searchLen);

        $pos = strpos($string, $search);

        if ($pos === false) {
            break;
        }
    }

    return $string;
}

使用示例:

$search  = 'foo';
$replace = 'bar';
$string  = 'foo wizard makes foo brew for evil foo and jack';
$limit   = 2;

$replaced = str_replace_limit($search, $replace, $string, $limit);

echo $replaced;
// bar wizard makes bar brew for evil foo and jack

答案 6 :(得分:3)

最简单的方法是使用正则表达式。

另一种方法是使用strpos()找到字符串的位置,然后找到substr_replace()

但我真的会选择RegExp。

答案 7 :(得分:3)

function str_replace_once($search, $replace, $subject) {
    $pos = strpos($subject, $search);
    if ($pos === false) {
        return $subject;
    }

    return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
}

答案 8 :(得分:2)

根据我的测试结果,我想投票给karim79提供的regular_express。 (我现在没有足够的声誉投票!)

zombat的解决方案使用了太多的函数调用,我甚至简化了代码。我使用PHP 5.4运行两个解决方案100,000次,结果如下:

$str = 'Hello abc, have a nice day abc! abc!';
$pos = strpos($str, 'abc');
$str = substr_replace($str, '123', $pos, 3);

==&GT; 1.85秒

$str = 'Hello abc, have a nice day abc! abc!';
$str = preg_replace('/abc/', '123', $str, 1);

==&GT; 1.35秒

如你所见。 preg_replace的性能并不像许多人想象的那么糟糕。如果您的常规快递并不复杂,我建议使用优雅的解决方案。

答案 9 :(得分:2)

补充人们所说的,请记住整个字符串是一个数组:

$string = "Lorem ipsum lá lá lá";

$string[0] = "B";

echo $string;

“Boremipsumlálálá”

答案 10 :(得分:2)

=&GT;代码已被修改,所以请考虑一些评论太旧

  

感谢所有人帮助我改进

     

任何BUG,请与我沟通; 我会在

之后立即解决这个问题

所以,让我们去:

将第一个'o'替换为'ea',例如:

$s='I love you';
echo str_replace_first('o','ea',$s);

//output: I leave you

功能:

function str_replace_first($a,$b,$s)
         {
         $w=strpos($s,$a);
         if($w===false)return $s;
         return substr($s,0,$w).$b.substr($s,$w+strlen($a));
         }

答案 11 :(得分:2)

为了扩展zombat的答案(我认为这是最好的答案),我创建了一个函数的递归版本,它接受$limit参数来指定你想要替换的次数。

function str_replace_limit($haystack, $needle, $replace, $limit, $start_pos = 0) {
    if ($limit <= 0) {
        return $haystack;
    } else {
        $pos = strpos($haystack,$needle,$start_pos);
        if ($pos !== false) {
            $newstring = substr_replace($haystack, $replace, $pos, strlen($needle));
            return str_replace_limit($newstring, $needle, $replace, $limit-1, $pos+strlen($replace));
        } else {
            return $haystack;
        }
    }
}

答案 12 :(得分:2)

为了扩展@renocor's answer,我编写了一个与str_replace() 100%向后兼容的函数。也就是说,您可以将str_replace()的{​​strong>所有次出现替换为str_replace_limit(),而不会弄乱任何内容,即使那些使用$search$replace的数组,和/或$subject

函数可以完全自包含,如果你想用($string===strval(intval(strval($string))))替换函数调用,但我建议反对它,因为valid_integer()是一个相当的处理作为字符串提供的整数时的有用函数。

注意: 只要有可能,str_replace_limit()将使用str_replace(),因此对str_replace()的所有调用都可以替换为str_replace_limit()而不用担心会遇到影响。

用法

<?php
$search = 'a';
$replace = 'b';
$subject = 'abcabc';
$limit = -1; // No limit
$new_string = str_replace_limit($search, $replace, $subject, $count, $limit);
echo $count.' replacements -- '.$new_string;
  

2个替换 - bbcbbc

$limit = 1; // Limit of 1
$new_string = str_replace_limit($search, $replace, $subject, $count, $limit);
echo $count.' replacements -- '.$new_string;
  

1替换 - bbcabc

$limit = 10; // Limit of 10
$new_string = str_replace_limit($search, $replace, $subject, $count, $limit);
echo $count.' replacements -- '.$new_string;
  

2个替换 - bbcbbc

功能

<?php

/**
 * Checks if $string is a valid integer. Integers provided as strings (e.g. '2' vs 2)
 * are also supported.
 * @param mixed $string
 * @return bool Returns boolean TRUE if string is a valid integer, or FALSE if it is not 
 */
function valid_integer($string){
    // 1. Cast as string (in case integer is provided)
    // 1. Convert the string to an integer and back to a string
    // 2. Check if identical (note: 'identical', NOT just 'equal')
    // Note: TRUE, FALSE, and NULL $string values all return FALSE
    $string = strval($string);
    return ($string===strval(intval($string)));
}

/**
 * Replace $limit occurences of the search string with the replacement string
 * @param mixed $search The value being searched for, otherwise known as the needle. An
 * array may be used to designate multiple needles.
 * @param mixed $replace The replacement value that replaces found search values. An
 * array may be used to designate multiple replacements.
 * @param mixed $subject The string or array being searched and replaced on, otherwise
 * known as the haystack. If subject is an array, then the search and replace is
 * performed with every entry of subject, and the return value is an array as well. 
 * @param string $count If passed, this will be set to the number of replacements
 * performed.
 * @param int $limit The maximum possible replacements for each pattern in each subject
 * string. Defaults to -1 (no limit).
 * @return string This function returns a string with the replaced values.
 */
function str_replace_limit(
        $search,
        $replace,
        $subject,
        &$count,
        $limit = -1
    ){

    // Set some defaults
    $count = 0;

    // Invalid $limit provided. Throw a warning.
    if(!valid_integer($limit)){
        $backtrace = debug_backtrace();
        trigger_error('Invalid $limit `'.$limit.'` provided to '.__function__.'() in '.
                '`'.$backtrace[0]['file'].'` on line '.$backtrace[0]['line'].'. Expecting an '.
                'integer', E_USER_WARNING);
        return $subject;
    }

    // Invalid $limit provided. Throw a warning.
    if($limit<-1){
        $backtrace = debug_backtrace();
        trigger_error('Invalid $limit `'.$limit.'` provided to '.__function__.'() in '.
                '`'.$backtrace[0]['file'].'` on line '.$backtrace[0]['line'].'. Expecting -1 or '.
                'a positive integer', E_USER_WARNING);
        return $subject;
    }

    // No replacements necessary. Throw a notice as this was most likely not the intended
    // use. And, if it was (e.g. part of a loop, setting $limit dynamically), it can be
    // worked around by simply checking to see if $limit===0, and if it does, skip the
    // function call (and set $count to 0, if applicable).
    if($limit===0){
        $backtrace = debug_backtrace();
        trigger_error('Invalid $limit `'.$limit.'` provided to '.__function__.'() in '.
                '`'.$backtrace[0]['file'].'` on line '.$backtrace[0]['line'].'. Expecting -1 or '.
                'a positive integer', E_USER_NOTICE);
        return $subject;
    }

    // Use str_replace() whenever possible (for performance reasons)
    if($limit===-1){
        return str_replace($search, $replace, $subject, $count);
    }

    if(is_array($subject)){

        // Loop through $subject values and call this function for each one.
        foreach($subject as $key => $this_subject){

            // Skip values that are arrays (to match str_replace()).
            if(!is_array($this_subject)){

                // Call this function again for
                $this_function = __FUNCTION__;
                $subject[$key] = $this_function(
                        $search,
                        $replace,
                        $this_subject,
                        $this_count,
                        $limit
                );

                // Adjust $count
                $count += $this_count;

                // Adjust $limit, if not -1
                if($limit!=-1){
                    $limit -= $this_count;
                }

                // Reached $limit, return $subject
                if($limit===0){
                    return $subject;
                }

            }

        }

        return $subject;

    } elseif(is_array($search)){
        // Only treat $replace as an array if $search is also an array (to match str_replace())

        // Clear keys of $search (to match str_replace()).
        $search = array_values($search);

        // Clear keys of $replace, if applicable (to match str_replace()).
        if(is_array($replace)){
            $replace = array_values($replace);
        }

        // Loop through $search array.
        foreach($search as $key => $this_search){

            // Don't support multi-dimensional arrays (to match str_replace()).
            $this_search = strval($this_search);

            // If $replace is an array, use the value of $replace[$key] as the replacement. If
            // $replace[$key] doesn't exist, just an empty string (to match str_replace()).
            if(is_array($replace)){
                if(array_key_exists($key, $replace)){
                    $this_replace = strval($replace[$key]);
                } else {
                    $this_replace = '';
                }
            } else {
                $this_replace = strval($replace);
            }

            // Call this function again for
            $this_function = __FUNCTION__;
            $subject = $this_function(
                    $this_search,
                    $this_replace,
                    $subject,
                    $this_count,
                    $limit
            );

            // Adjust $count
            $count += $this_count;

            // Adjust $limit, if not -1
            if($limit!=-1){
                $limit -= $this_count;
            }

            // Reached $limit, return $subject
            if($limit===0){
                return $subject;
            }

        }

        return $subject;

    } else {
        $search = strval($search);
        $replace = strval($replace);

        // Get position of first $search
        $pos = strpos($subject, $search);

        // Return $subject if $search cannot be found
        if($pos===false){
            return $subject;
        }

        // Get length of $search, to make proper replacement later on
        $search_len = strlen($search);

        // Loop until $search can no longer be found, or $limit is reached
        for($i=0;(($i<$limit)||($limit===-1));$i++){

            // Replace 
            $subject = substr_replace($subject, $replace, $pos, $search_len);

            // Increase $count
            $count++;

            // Get location of next $search
            $pos = strpos($subject, $search);

            // Break out of loop if $needle
            if($pos===false){
                break;
            }

        }

        // Return new $subject
        return $subject;

    }

}

答案 13 :(得分:2)

$string = 'this is my world, not my world';
$find = 'world';
$replace = 'farm';
$result = preg_replace("/$find/",$replace,$string,1);
echo $result;

答案 14 :(得分:1)

对于字符串

$string = 'OOO.OOO.OOO.S';
$search = 'OOO';
$replace = 'B';

//replace ONLY FIRST occurance of "OOO" with "B"
    $string = substr_replace($string,$replace,0,strlen($search));
    //$string => B.OOO.OOO.S

//replace ONLY LAST occurance of "OOOO" with "B"
    $string = substr_replace($string,$replace,strrpos($string,$search),strlen($search)) 
    //$string => OOO.OOO.B.S

    //replace ONLY LAST occurance of "OOOO" with "B"
    $string = strrev(implode(strrev($replace),explode(strrev($search),strrev($string),2)))
    //$string => OOO.OOO.B.S

对于单个角色

$string[strpos($string,$search)] = $replace;


//EXAMPLE

$string = 'O.O.O.O.S';
$search = 'O';
$replace = 'B';

//replace ONLY FIRST occurance of "O" with "B" 
    $string[strpos($string,$search)] = $replace;  
    //$string => B.O.O.O.S

//replace ONLY LAST occurance of "O" with "B" 
    $string[strrpos($string,$search)] = $replace; 
    // $string => B.O.O.B.S

答案 15 :(得分:1)

$str = "/property/details&id=202&test=123#tab-6p";
$position = strpos($str,"&");
echo substr_replace($str,"?",$position,1);

使用substr_replace,我们只能替换字符串中第一个字符的出现。 由于&会重复多次,但只有在第一个位置,我们才必须将&替换为?

答案 16 :(得分:0)

您可以使用:

function str_replace_once($str_pattern, $str_replacement, $string){ 

        if (strpos($string, $str_pattern) !== false){ 
            $occurrence = strpos($string, $str_pattern); 
            return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern)); 
        } 

        return $string; 
    } 

从php.net发现这个例子

用法:

$string = "Thiz iz an examplz";
var_dump(str_replace_once('z','Z', $string)); 

输出:

ThiZ iz an examplz

这可能会降低性能,但最简单的解决方案。

答案 17 :(得分:0)

如果您的字符串不包含任何多字节字符,并且您只想替换一个字符,则只需使用strpos

这是一个处理错误的函数

/**
 * Replace the first occurence of given string
 *
 * @param  string $search  a char to search in `$subject`
 * @param  string $replace a char to replace in `$subject`
 * @param  string $subject
 * @return string
 *
 * @throws InvalidArgumentException if `$search` or `$replace` are invalid or if `$subject` is a multibytes string
 */
function str_replace_first(string $search , string $replace , string $subject) : string {
    // check params
    if(strlen($replace) != 1 || strlen($search) != 1) {
        throw new InvalidArgumentException('$search & $replace must be char');
    }elseif(mb_strlen($subject) != strlen($subject)){
        throw new InvalidArgumentException('$subject is an multibytes string');
    }
    // search 
    $pos = strpos($subject, $search);
    if($pos === false) {
        // not found
        return $subject;
    }

    // replace
    $subject[$replace] = $subject;

    return $subject;
}

答案 18 :(得分:0)

用于循环解决方案

<?php
echo replaceFirstMatchedChar("&", "?", "/property/details&id=202&test=123#tab-6");

function replaceFirstMatchedChar($searchChar, $replaceChar, $str)
{
    for ($i = 0; $i < strlen($str); $i++) {

        if ($str[$i] == $searchChar) {
            $str[$i] = $replaceChar;
            break;
        }
    }
    return $str;
}

答案 19 :(得分:-1)

这是我创建的一个简单的类,用于包装我们稍微修改过的str_replace()函数。

我们的php :: str_rreplace()函数还允许你执行一个反向的,有限的str_replace(),它在尝试只替换字符串的最后X个实例时非常方便。

这些示例都使用preg_replace()

<?php
class php {

    /**
    * str_replace() from the end of a string that can also be limited e.g. replace only the last instance of '</div>' with ''
    *
    * @param string   $find
    * @param string   $replace
    * @param string   $subject
    * @param int      $replacement_limit | -1 to replace all references
    *
    * @return string
    */
    public static function str_replace($find, $replace, $subject, $replacement_limit = -1) {
        $find_pattern = str_replace('/', '\/', $find);
        return preg_replace('/' . $find_pattern . '/', $replace, $subject, $replacement_limit);
    }

    /**
    * str_replace() from the end of a string that can also be limited e.g. replace only the last instance of '</div>' with ''
    *
    * @param string   $find
    * @param string   $replace
    * @param string   $subject
    * @param int      $replacement_limit | -1 to replace all references
    *
    * @return string
    */
    public static function str_rreplace($find, $replace, $subject, $replacement_limit = -1) {
        return strrev( self::str_replace(strrev($find), strrev($replace), strrev($subject), $replacement_limit) );
    }
}

答案 20 :(得分:-3)

这是我的第一个答案,我希望能够正确地做到这一点。 为什么不使用str_replace函数的第四个参数来解决这个问题?

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
  

count:如果通过,则将设置为执行的替换次数。


编辑:这个答案是错误的,因为str_replace的第4个参数是一个变量,它被分配了完成的替换次数。这与preg_replace不一致,{{3}}有第4个参数$limit和第5个参数&$count

答案 21 :(得分:-5)

很容易找到只替换第一个或第一个实例的解决方案(通过给出计数值)。 没有多少解决方案可以替换最后一个或最后一个实例。

也许类似str_replace($ find,$ replace,$ subject,-3)应该替换最后三个实例。

无论如何,只是一个建议。