Home >Backend Development >PHP Tutorial >php str_replace function (can limit the number of replacements)_PHP tutorial

php str_replace function (can limit the number of replacements)_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:02:00957browse

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:


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

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445401.htmlTechArticlephp str_replace function (can limit the number of replacements) This article mainly introduces the str_replace function to replace a single character and replace data , and specify the number of str_replace replacements, the last replacement...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn