国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php str_replace函數,可限制替換個數

2024-09-04 11:48:57
字體:
來源:轉載
供稿:網友

本文章主要是介紹str_replace函數替換單個字符,與替換數據,以及指定str_replace替換的次數,最后一個替換次數是很有用的特別是在seo優化方面.

mixed str_replace(mixed $search , mixed $replace , mixed $subject [,int &$count ] )

函數返回一個字符串或在出現問題的所有搜索給定的數組替換值替換,如果你不喜歡需要更換的規則(如正則表達式),你應該始終使用此函數,而不是ereg_replace()或preg_replace函數(),實例代碼如下:

  1. */ 
  2. // provides: <body text='black'> 
  3. $bodytag = str_replace("%body%""black""<body text='%body%'>"); 
  4. // provides: hll wrld f php 
  5. $vowels = array("a""e""i""o""u""a""e""i""o""u"); 
  6. $onlyconsonants = str_replace($vowels"""hello world of php"); 
  7. // provides: you should eat pizza, beer, and ice cream every day 
  8. $phrase  = "you should eat fruits, vegetables, and fiber every day."
  9. $healthy = array("fruits""vegetables""fiber"); 
  10. $yummy   = array("pizza""beer""ice cream"); 
  11. $newphrase = str_replace($healthy$yummy$phrase); 
  12. // provides: 2 
  13. $str = str_replace("ll""""good golly miss molly!"$count); 
  14. echo $count
  15. //利用str_replace函數指定替換次數 
  16. $array = array
  17.  array(0,1,2) 
  18.  ); 
  19. function keywords( $str,$array ) 
  20.  $count =0; 
  21.  foreach($array as $v){      
  22.   if(strstr($str,strtolower($v[0]))!==false){        
  23.    if$count <=3 ){ 
  24.     $tos = strtolower($v[0]); 
  25.     $str=preg_replace("/$tos/","<a href=".$v[1]." target=_blank >".$v[2]."</a>",$str,1);//開源軟件:Vevb.com 
  26.     $count++; 
  27.     continue
  28.    } 
  29.   }     
  30.  } 
  31.  return $str;   
  32. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安阳县| 涿州市| 霸州市| 年辖:市辖区| 平舆县| 盐源县| 阳朔县| 遂昌县| 贵州省| 道真| 平湖市| 梁山县| 大新县| 桂平市| 丰镇市| 日土县| 祁东县| 仙游县| 云林县| 海阳市| 策勒县| 海林市| 枝江市| 拜城县| 隆安县| 平原县| 汕尾市| 盐源县| 南召县| 明光市| 沙湾县| 和平县| 伊金霍洛旗| 玉林市| 乌兰浩特市| 曲阜市| 博白县| 图木舒克市| 炉霍县| 宁夏| 崇明县|