Replace substrings with an incremented counter value

后端 未结 5 1880
耶瑟儿~
耶瑟儿~ 2021-01-17 08:25

Basically what I\'m looking for is the PHP version of this thread: Find, replace, and increment at each occurence of string

I would like to replace the keyword follow

5条回答
  •  失恋的感觉
    2021-01-17 09:26

    Here's my two cents

    function str_replace_once($correct, $wrong, $haystack) {
        $wrong_string = '/' . $wrong . '/';
        return preg_replace($wrong_string, $correct, $haystack, 1);
    }
    

    The above function is used to replace the string occurence only once, but you are free to edit the function to perform every other possible operation.

提交回复
热议问题