This article is mainly introduced str_replace function to replace a single character, and replace the data, and specify the number of str_replace replacement, the last replacement is very useful, especially in the SEO optimization methods.
/ mixed str_replace (mixed $ search, mixed $ replace, mixed $ subject [, int & $ count])
A function returns a string or replacement value for the array-replacement value given in all searches for the problem.
If you do not like rules that need to be replaced (like regular expressions), you should always use this function instead of the ereg_replace () or preg_replace () function.
* /
// provides: <body text = 'black'>
$ bodytag = str_replace ("% body%", "black", "<body text = '% body%'>");
// 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 should eat pizza, beer, and ice cream every day
$ phrase = "you should 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 str_replace function to specify the number of substitutions
$ 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 /", "<a href=".$v[1]." target=_blank>". $ v [2]. "</a>", $ str, 1) ;
$ count ++;
continue
}
}
}
return $ str;
}
?>