Php replacement once or N times. php replacement _ PHP Tutorial

Source: Internet
Author: User
Php can be replaced only once or N times. Php can be replaced only once or N times. we all know that in php, Strtr, strreplace, and other functions can be replaced, but they all replace each replacement, how can I replace php once or N times?

We all know that Strtr, strreplace, and other functions can be used to replace in PHP, but they are all replaced each time they are replaced. for example:
"Abcabbc": if the above function is used to replace B, all B is replaced. But what if you want to replace only one or two? See the solution below:
This is a bit interesting, and I have done similar processing before. at that time, I directly implemented it using preg_replace.

Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Search for matches in pattern in subject and replace them with replacement. If limit is specified, only limit matches are replaced. if limit is omitted or its value is-1, all matches are replaced.
Because the fourth parameter of preg_replace can limit the number of replicas, it is very convenient to solve this problem. However, after viewing the comment on the str_replace function on php.net, you can also pick out several representative functions.

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

<?phpfunction 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 foundreturn $haystack;}return substr_replace($haystack, $replace, $pos, strlen($needle));}?>

Method 2: str_replace_limit
IdeasWe still use preg_replace, but its parameters are more like preg_replace, and some special characters are escaped for better universality.

<?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,'`') . '`';}// replacementreturn preg_replace($search, $replace, $subject, $limit);}?>

You can combine the article php keyword replacement function, which was previously compiled by Xiaobian, to learn from it. I believe you will have unexpected gains.

We all know that Strtr, strreplace, and other functions can be used to replace in PHP, but they all replace each time they replace. for example...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.