欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php关键词替换的类(避免重复替换,保留与还原原始链接)

程序员文章站 2022-05-20 14:07:20
...
  1. /*

  2. * 关键词匹配类
  3. * @author ylx
  4. * @packet mipang
  5. * 使用实例
  6. * $str = "绿壳蛋鸡撒范德萨下一年,下一年1的洒落开房间卢卡斯地方军";
  7. * $key = new KeyReplace($str,array("xxxx"=>"sadf","下一年1"=>'http://baidu.com',"下一年"=>'google.com'));
  8. * echo $key->getResultText();
  9. * echo $key->getRuntime();
  10. */
  11. class KeyReplace
  12. {
  13. private $keys = array();
  14. private $text = "";
  15. private $runtime = 0;
  16. private $url = true;
  17. private $stopkeys = array();
  18. private $all = false;
  19. /**
  20. * @access public
  21. * @param string $text 指定被处理的文章
  22. * @param array $keys 指定字典词组array(key=>url,...) url可以是数组,如果是数组将随机替换其中的一个
  23. * @param array $stopkeys 指定停止词array(key,...) 这里面的词将不会被处理
  24. * @param boolean $url true 表示替换成链接否则只替换
  25. * @param boolean $all true 表示替换所有找到的词,否则只替换第一次
  26. */
  27. public function __construct($text='',$keys=array(),$url=true,$stopkeys=array(),$all=false) {
  28. $this->keys = $keys;
  29. $this->text = $text;
  30. $this->url = $url;
  31. $this->stopkeys = $stopkeys;
  32. $this->all = $all;
  33. }
  34. /**

  35. * 获取处理好的文章
  36. * @access public
  37. * @return string text
  38. */
  39. public function getResultText() {
  40. $start = microtime(true);
  41. $keys = $this->hits_keys();
  42. $keys_tmp = array_keys($keys);

  43. function cmp($a, $b){

  44. if (mb_strlen($a) == mb_strlen($b)) {
  45. return 0;
  46. }
  47. return (mb_strlen($a) }
  48. usort($keys_tmp,"cmp");

  49. foreach($keys_tmp as $key){

  50. if(is_array($keys[$key])){

  51. $url = $keys[$key][rand(0,count($keys[$key])-1)];
  52. }else
  53. $url = $keys[$key];
  54. $this->text = $this->r_s($this->text,$key,$url);

  55. }

  56. $this->runtime = microtime(true)-$start;
  57. return $this->text;

  58. }
  59. /**
  60. * 获取处理时间
  61. * @access public
  62. * @return float
  63. */
  64. public function getRuntime() {
  65. return $this->runtime;

  66. }

  67. /**

  68. * 设置关键词
  69. * @access public
  70. * @param array $keys array(key=>url,...)
  71. */
  72. public function setKeys($keys) {
  73. $this->keys = $keys;

  74. }

  75. /**
  76. * 设置停止词
  77. * @access public
  78. * @param array $keys array(key,...)
  79. */
  80. public function setStopKeys($keys) {
  81. $this->stopkeys = $keys;

  82. }

  83. /**
  84. * 设置文章
  85. * @access public
  86. * @param string $text
  87. */
  88. public function setText($text) {
  89. $this->text = $text;

  90. }

  91. /**

  92. * 用来找到字符串里面命中的关键词
  93. * @access public
  94. * @return array $keys 返回匹配到的词array(key=>url,...)
  95. */
  96. public function hits_keys(){
  97. $ar = $this->keys;
  98. $ar = $ar?$ar:array();
  99. $result=array();
  100. $str = $this->text;
  101. foreach($ar as $k=>$url){
  102. $k = trim($k);
  103. if(!$k)
  104. continue;
  105. if(strpos($str,$k)!==false && !in_array($k,$this->stopkeys)){
  106. $result[$k] = $url;
  107. }
  108. }
  109. return $result?$result:array();
  110. }
  111. /**

  112. * 用来找到字符串里面命中的停止词
  113. * @access public
  114. * @return array $keys 返回匹配到的词array(key,...)
  115. */
  116. public function hits_stop_keys(){
  117. $ar = $this->stopkeys;
  118. $ar = $ar?$ar:array();
  119. $result=array();
  120. $str = $this->text;
  121. foreach($ar as $k){
  122. $k = trim($k);
  123. if(!$k)
  124. continue;
  125. if(strpos($str,$k)!==false && in_array($k,$this->stopkeys)){
  126. $result[] = $k;
  127. }
  128. }
  129. return $result?$result:array();
  130. }
  131. /**

  132. * 处理替换过程
  133. * @access private
  134. * @param string $text 被替换者
  135. * @param string $key 关键词
  136. * @param string $url 链接
  137. * @return string $text 处理好的文章
  138. */
  139. private function r_s($text,$key,$url){
  140. $tmp = $text;

  141. $stop_keys = $this->hits_stop_keys();

  142. $stopkeys = $tags = $a = array();

  143. if(preg_match_all("#]+>[^]*>#su",$tmp,$m)){
  144. $a=$m[0];
  145. foreach($m[0] as $k=>$z){

  146. $z = preg_replace("#\##s","\#",$z);
  147. $tmp = preg_replace('#'.$z.'#s',"[_a".$k."_]",$tmp,1);

  148. }
  149. };

  150. if(preg_match_all("#]+>#s",$tmp,$m)){

  151. $tags = $m[0];
  152. foreach($m[0] as $k=>$z){
  153. $z = preg_replace("#\##s","\#",$z);
  154. $tmp = preg_replace('#'.$z.'#s',"[_tag".$k."_]",$tmp,1);
  155. }
  156. }
  157. if(!empty($stop_keys)){
  158. if(preg_match_all("#".implode("|",$stop_keys)."#s",$tmp,$m)){
  159. $stopkeys = $m[0];
  160. foreach($m[0] as $k=>$z){
  161. $z = preg_replace("#\##s","\#",$z);
  162. $tmp = preg_replace('#'.$z.'#s',"[_s".$k."_]",$tmp,1);
  163. }
  164. }
  165. }
  166. $key1 = preg_replace("#([\#\(\)\[\]\*])#s","\\\\$1",$key);
  167. if($this->url)

  168. $tmp = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",''.$key.'',$tmp,$this->all?-1:1);
  169. else
  170. $tmp = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",$url,$tmp,$this->all?-1:1);
  171. if(!empty($a)){

  172. foreach($a as $n=>$at){

  173. $tmp = str_replace("[_a".$n."_]",$at,$tmp);

  174. }

  175. }

  176. if(!empty($tags)){
  177. foreach($tags as $n=>$at){

  178. $tmp = str_replace("[_tag".$n."_]",$at,$tmp);

  179. }

  180. }

  181. if(!empty($stopkeys)){
  182. foreach($stopkeys as $n=>$at){

  183. $tmp = str_replace("[_s".$n."_]",$at,$tmp);

  184. }

  185. }

  186. return $tmp;
  187. }
  188. }
复制代码