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;
}
?>