PHP content keyword replacement function sharing

WBOY
Release: 2016-07-25 08:57:36
Original
847 people have browsed it
  1. /**

  2. * Content keyword replacement
  3. * by bbs.it-home.org
  4. */

  5. $arr=array(

  6. "shoes"=>"Shoes",
  7. "Socks"=>"Socks",
  8. "Skirt"=> ;"skirt",
  9. "dress"=>"dress",
  10. );
  11. $str="shoes socks shoes socks shoes socks shoes socks skirt dress";
  12. $str=replacelink($str,$arr,3);
  13. echo $str;

  14. public function replacelink($str,$arr,$maxcount=3000){

  15. //Match the picture
  16. preg_match_all("/(]*>)/iUs",$str,$imgs) ;
  17. preg_match_all("/(.*)/iUs",$str,$links);
  18. //Replace pictures and links
  19. if($links[1]){
  20. foreach($links[1] as $k=>$v){
  21. $str=str_replace($v,"@links_$k",$str);
  22. }
  23. }
  24. if($imgs[1] ){
  25. foreach($imgs[1] as $k=>$v){
  26. $str=str_replace($v,"@imgs_$k",$str);
  27. }
  28. }
  29. //End of matching
  30. $str=strtr($str,$arr);
  31. preg_match_all("/(.*)/iUs",$str,$a);
  32. $a=$a [1];
  33. $c=array_count_values($a);
  34. if($c){
  35. $kk=0;
  36. foreach($c as $k=>$v){
  37. if($v){
  38. if($kk<$maxcount){
  39. $v=$v-1;
  40. }
  41. $str=preg_replace("/".$this->replace_quote($k)."/i",str_replace("# ","",strip_tags($k)),$str,$v);
  42. }
  43. $kk++;
  44. }
  45. }
  46. //Restore pictures and links
  47. if($links[1]){
  48. foreach($ links[1] as $k=>$v){
  49. $str=str_replace("@links_$k",$v,$str);
  50. }
  51. }
  52. if($imgs[1]){
  53. foreach ($imgs[1] as $k=>$v){
  54. $str=str_replace("@imgs_$k",$v,$str);
  55. }
  56. }
  57. return $str;
  58. }
  59. function replace_quote($str){

  60. $str=preg_quote($str);
  61. $str=str_replace("/","/",$str);
  62. return $str;
  63. }< ;/p>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!