Home  >  Article  >  php教程  >  php str_replace 只匹配一次

php str_replace 只匹配一次

PHP中文网
PHP中文网Original
2016-05-25 17:08:271612browse

php str_replace 只匹配一次

用户可能是在找如何利用 php 的str_replace只替换目标字符串的内容一次,而不是全部替换。

function str_replace_once($needle, $replace, $haystack) {
    // Looks for the first occurence of $needle in $haystack
    // and replaces it with $replace.
    $pos = strpos($haystack, $needle);
    if ($pos === false) {
        // Nothing found
        return $haystack;
    }
    return substr_replace($haystack, $replace, $pos, strlen($needle));
}

                   

 以上就是php str_replace 只匹配一次的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn