php str_replace函數(可限制替換個數)_PHP教程

來源:互聯網
上載者:User
php str_replace函數(可限制替換個數)本文章主要是介紹str_replace函數替換單個字元,與替換資料,以及指定str_replace替換的次數,最後一個替換次數是很有用的特別是在seo最佳化方法面哦。

php教程 str_replace函數(可限制替換個數)
本文章主要是介紹str_replace函數替換單個字元,與替換資料,以及指定str_replace替換的次數,最後一個替換次數是很有用的特別是在seo教程最佳化方法面哦。
/mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

個函數返回一個字串或在出現問題的所有搜尋給定的數組替換值替換。

如果你不喜歡需要更換的規則(如Regex),你應該始終使用此函數,而不是ereg_replace()或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;

//利用str_replace函數指定替換次數

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

?>


http://www.bkjia.com/PHPjc/445401.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445401.htmlTechArticlephp str_replace函數(可限制替換個數)本文章主要是介紹str_replace函數替換單個字元,與替換資料,以及指定str_replace替換的次數,最後一個替換...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.