Home > php教程 > PHP源码 > body text

php str_replace 只匹配一次

PHP中文网
Release: 2016-05-25 17:08:27
Original
1617 people have browsed it

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));
}
Copy after login

                   

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template