Home > Backend Development > PHP Tutorial > str_replace method to replace a string only once_PHP tutorial

str_replace method to replace a string only once_PHP tutorial

WBOY
Release: 2016-07-21 15:12:25
Original
959 people have browsed it

We all know that in PHP, Strtr, strreplace and other functions can be used to replace, but they replace all of them every time. For example:
"abcabbc", if this string is used as above function to replace b among them, then it will replace them all, but what if you want to replace only one or two? See the solution below:
This is a rather interesting question, just before I have also done similar processing. At that time, I directly used preg_replace to implement it.

mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit] )
Search for a match of the pattern pattern in subject and replace it with replacement. If limit is specified, only limit matches are replaced; if limit is omitted or has a value of -1, all matches are replaced.
Because the fourth parameter of preg_replace can limit the number of replacements, it is very convenient to deal with this problem in this way. But after looking at the function comments about str_replace on php.net, we can actually pick out a few representative functions.

str_replace_once
The idea is to first find the location of the keyword to be replaced, and then use the substr_replace function to directly replace it.

Copy code The code is as follows:

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

// Looks for the first occurrence of $needle in $haystack

// and replaces it with $replace.

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

if ($pos === false) {

return $haystack;

}

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

}

?>

str_replace_limit
Still uses preg_replace, but its parameters are more like preg_replace, and some special characters are escaped, making it more versatile.

Copy code The code is as follows:

function str_replace_limit($search, $replace, $subject, $limit=-1) {

// constructing mask(s)...

if (is_array($search)) {

foreach ($search as $k=>$v) {

$search[$k] = ''' . preg_quote($search[$k],''') . ''';

}

}

else {

$search = '`' . preg_quote($search,''') . '`';

}

//replacement

return preg_replace($search, $replace, $subject, $limit);

}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326666.htmlTechArticleWe all know that in PHP, Strtr, strreplace and other functions can be used for replacement, but they replace each time Sometimes all are replaced, for example: "abcabbc", if this string uses the above...
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