Home > Backend Development > PHP Tutorial > How to implement replacement only once or N times in php, php replacement_PHP tutorial

How to implement replacement only once or N times in php, php replacement_PHP tutorial

WBOY
Release: 2016-07-12 09:06:03
Original
997 people have browsed it

How to achieve php replacement only once or N times, php replacement

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

mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
Searches the subject for a match of pattern and replaces 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.

Method 1: str_replace_once
The ideaFirst find the location of the keyword to be replaced, and then use the substr_replace function to directly replace it.

<&#63;php
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));
}
&#63;>
Copy after login

Method 2, str_replace_limit
The ideaStill uses preg_replace, but its parameters are more like preg_replace, and some special characters are escaped, making it more versatile.

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

You can study together with an article compiled by the editor before, "Implementation Function of PHP Keyword Replacement Only Once". I believe you will have unexpected gains.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1066506.htmlTechArticleHow to replace only once or N times in php, we all know about php replacement, Strtr, strreplace and other functions in PHP All can be used to replace, but they will replace them all every time. For example...
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