本文章主要是介紹str_replace函數(shù)替換單個字符,與替換數(shù)據(jù),以及指定str_replace替換的次數(shù),最后一個替換次數(shù)是很有用的特別是在seo優(yōu)化方面.
mixed str_replace(mixed $search , mixed $replace , mixed $subject [,int &$count ] )
函數(shù)返回一個字符串或在出現(xiàn)問題的所有搜索給定的數(shù)組替換值替換,如果你不喜歡需要更換的規(guī)則(如正則表達式),你應(yīng)該始終使用此函數(shù),而不是ereg_replace()或preg_replace函數(shù)(),實例代碼如下:
轉(zhuǎn)載請注明來源:php str_replace函數(shù),可限制替換個數(shù)
- */
- // 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;
- //利用str_replace函數(shù)指定替換次數(shù)
- $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 >.$v[1]." target=_blank >".$v[2]."</a>",$str,1);//開源軟件:phpfensi.com
- $count++;
- continue;
- }
- }
- }
- return $str;
- }
哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。
