PHP's original str_replace function replaces everything. If we want to replace only once, we can refer to the following implementation program to solve the problem.
str_replace() function uses a string to replace other characters in a string.
Grammar
str_replace(find,replace,string,count)
参数 |
描述 |
find |
必需。规定要查找的值。 |
replace |
必需。规定替换 find 中的值的值。 |
string |
必需。规定被搜索的字符串。 |
count |
可选。一个变量,对替换数进行计数。 |
PHP's one-time replacement function:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
function str_replace_once($needle, $replace, $haystack) {
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
|
function str_replace_once($needle, $replace, $haystack) {
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
|
http://www.bkjia.com/PHPjc/445292.htmltruehttp: //www.bkjia.com/PHPjc/445292.htmlTechArticlephp’s original str_replace function replaces all. If we want to replace only once, we can refer to the following implementation program to solve the problem . The str_replace() function uses a string to replace the string...