php text replacement specified times

*文
Release: 2023-03-18 16:50:01
Original
2083 people have browsed it

This article mainly introduces how to replace only once or only N times in PHP, and introduces the theme through a simple example. I hope to be helpful.

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", this string If you use the function above to replace b, 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. , 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] ## Search for the matching items of Pattern mode and replaced it to 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
IdeaFirst, find the location of the keyword to be replaced, and then use the substr_replace function to directly replace it .


<?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));
}
?>
Copy after login


Method 2, str_replace_limit
Ideas Still use preg_replace, but its parameters are more like preg_replace, and some special characters are escaped, making it more versatile.


<?
function str_replace_limit($search, $replace, $subject, $limit=-1) {
// constructing mask(s)...
if (is_array($search)) {
foreach ($search as $k=>$v) {
$search[$k] = &#39;`&#39; . preg_quote($search[$k],&#39;`&#39;) . &#39;`&#39;;
}
}
else {
$search = &#39;`&#39; . preg_quote($search,&#39;`&#39;) . &#39;`&#39;;
}
// replacement
return preg_replace($search, $replace, $subject, $limit);
}
?>
Copy after login


Related recommendations:

php Introduction to string segmentation and comparison

php Detailed explanation of the use of string regular replacement function preg_replace

php string replacement method

The above is the detailed content of php text replacement specified times. For more information, please follow other related articles on the PHP Chinese website!

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