Using str_replace so that it only acts on the first match?

后端 未结 22 936
醉酒成梦
醉酒成梦 2020-11-22 11:03

I want a version of str_replace() that only replaces the first occurrence of $search in the $subject. Is there an easy solution to thi

相关标签:
22条回答
  • 2020-11-22 11:53

    Here's a simple class I created to wrap our slightly modified str_replace() functions.

    Our php::str_rreplace() function also allows you to carry out a reverse, limited str_replace() which can be very handy when trying to replace only the final X instance(s) of a string.

    These examples both use 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) );
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:54
    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));
    }
    
    0 讨论(0)
  • 2020-11-22 11:56
    $string = 'this is my world, not my world';
    $find = 'world';
    $replace = 'farm';
    $result = preg_replace("/$find/",$replace,$string,1);
    echo $result;
    
    0 讨论(0)
  • 2020-11-22 11:58

    According to my test result, I'd like to vote the regular_express one provided by karim79. (I don't have enough reputation to vote it now!)

    The solution from zombat uses too many function calls, I even simplify the codes. I'm using PHP 5.4 to run both solutions for 100,000 times, and here's the result:

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

    ==> 1.85 sec

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

    ==> 1.35 sec

    As you can see. The performance of preg_replace is not so bad as many people think. So I'd suggest the classy solution if your regular express is not complicated.

    0 讨论(0)
提交回复
热议问题