Php str_replace function (Limit the number of replicas)

Source: Internet
Author: User

Php str_replace function (maximum number of replicas) This article mainly introduces how to replace a single character, replace data, and specify the number of replace str_replace, the last replacement is very useful, especially in seo optimization methods.

Php tutorial str_replace function (Limit the number of replicas)
This article mainly introduces how to replace a single character with the str_replace function, replace data, and specify the number of replace Replace str_replace. The last replace is very useful, especially in the seo tutorial optimization method.
/Mixed str_replace (mixed $ search, mixed $ replace, mixed $ subject [, int & $ count])

Return a string or replace the given array replacement value in all searches in case of a problem.

If you do not like rules to be replaced (such as regular expressions), you should always use this function, instead of the ereg_replace () or preg_replace function ().
*/
// Provides:


$ Bodytag = str_replace ("% body %", "black ","");

// Provides: hll wrld f php
$ Vowels = array ("a", "e", "I", "o", "u", "a", "e", "I ", "o", "u ");
$ Onlyconsonants = str_replace ($ vowels, "", "hello world of php ");

// Provides: you shoshould eat pizza, beer, and ice cream every day
$ Phrase = "you shoshould eat fruits, vegetables, and fiber every day .";
$ Healthy = array ("fruits", "vegetables", "fiber ");
$ Yummy = array ("pizza", "beer", "ice cream ");

$ Newphrase = str_replace ($ healthy, $ yummy, $ phrase );

// Provides: 2
$ Str = str_replace ("ll", "", "good golly miss molly! ", $ Count );
Echo $ count;

// Use the str_replace function to specify the number of replicas

$ Array = array (
Array (0, 1, 2)
);
Function keywords ($ str, $ array)
{
$ Count = 0;
Foreach ($ array as $ v ){
If (strstr ($ str, strtolower ($ v [0])! = False ){
If ($ count <= 3 ){
$ Tos = strtolower ($ v [0]);
$ Str = preg_replace ("/$ tos/", "". $ v [2]. "", $ str, 1 );
$ Count ++;
Continue;
}
}
}
Return $ str;
}

?>


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.