Home > php教程 > php手册 > body text

php中str_replace函数只替换一次实例

WBOY
Release: 2016-06-13 11:19:40
Original
1450 people have browsed it

php原来的str_replace函数是替换全部的,如果我们要只替换一次的话可参考下面实现程序来解决。  

str_replace() 函数使用一个字符串替换字符串中的另一些字符。

语法
str_replace(find,replace,string,count)

参数 描述
find 必需。规定要查找的值。
replace 必需。规定替换 find 中的值的值。
string 必需。规定被搜索的字符串。
count 可选。一个变量,对替换数进行计数。


php的替换一次的函数:

 代码如下 复制代码

function str_replace_once($needle, $replace, $haystack) {

$pos = strpos($haystack, $needle);

if ($pos === false) {

return $haystack;

}

return substr_replace($haystack, $replace, $pos, strlen($needle));

}


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!