Home > Article > Backend Development > php str_replace function (can limit the number of replacements)_PHP tutorial
PHP str_replace function (can limit the number of replacements) This article mainly introduces the str_replace function to replace a single character, replace data, and specify the number of str_replace replacements. The last number of replacements is very useful, especially in SEO optimization methods.
php tutorial str_replace function (can limit the number of replacements)
This article mainly introduces the str_replace function to replace a single character, replace data, and specify the number of str_replace substitutions. The last number of substitutions is very useful, especially in SEO tutorial optimization methods.
/mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
This function returns a string or replaces the given array with a replacement value in all occurrences of the search.
If you don't like rules that require replacement (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().
*/
// provides:
// 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 the str_replace function to specify the number of replacements
$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;
}
?>