Home > Backend Development > PHP Tutorial > The str_replace function in php only replaces an instance once_PHP tutorial

The str_replace function in php only replaces an instance once_PHP tutorial

WBOY
Release: 2016-07-20 11:03:17
Original
1255 people have browsed it

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.html
www.bkjia.com
truehttp: //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...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template