PHPIN.NET

 找回密码
 立即注册
查看: 6653|回复: 0

[PHP类\函数] PHP实现文章内容关键词加链接类

[复制链接]

469

主题

31

回帖

5497

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5497
发表于 2016-6-17 08:53:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
PHP实现文章内容关键词加链接类

实现文章内关键词自动加链接(用于内链SEO等),长词优先替换,替换过的不会二次替换,可设置替换数量(一个关键词允许出现N次)等。
直接上源码:
HotWords.class.php:
  1. <?php

  2. /**
  3. * @abstract 内容热词(<a >热词</a>除外)加超链功能类
  4. * @param string $content 内容
  5. * @param array $keyWords 二维数组,格式: array(
  6. *                                          array('热词1', 'URL1'),
  7. *                                          array('热词2', 'URL2'),
  8. *                                          ...
  9. *                                          array('热词n', 'URLn')
  10. *                                      );
  11. * @param int $times 同一个热词在内容中前$times次都将被加上超链
  12. * @author WKD
  13. *
  14. */

  15. class HotWords{
  16.     protected $_content;
  17.     protected $_keyWords;
  18.     protected $_times;
  19.    
  20.     public function __construct($content, $keyWords, $times){
  21.         $this->setContent($content);
  22.         $this->setKeyWords($keyWords);
  23.         $this->setTimes($times);
  24.     }
  25.    
  26.     /**
  27.      * @abstract 给热词加链接
  28.      * @access public
  29.      */
  30.     public function addHotLink(){
  31.         /**
  32.          *  思路说明:
  33.          *      1、对关键词数组进行排序,长的关键词排到前面,这样就可以先替换长的关键词
  34.          *      2、提取出所有html标签,暂存到数组里(<a>****</a>整体提取)
  35.          *      3、根据html标签对内容进行分割,将分割的内容保存到一个数组里
  36.          *      4、将没有了html标签的内容数组,用一个“特殊字符(临时替换html标签的中间变量$tempReplaeHtmlTag)”拼接成一个字符串
  37.          *      5、先用加密的热词替换4中得到的字符串中的热词,再用热词本身替换加密的热词
  38.          *      6、根据“特殊字符(临时替换html标签的中间变量)”将5中得到的字符串换成数组
  39.          *      7、拼接6中得到的数组与2中得到的html标签数组,即可。
  40.          */
  41.         
  42.         $content = $this->getContent();                             //得到文章内容
  43.         $kWordsArr = $this->keyWordSort($this->getKeyWords());      //对热词进行排序
  44.         $htmlTagArr = $this->getAllHtmlTag($content);               //得到内容中的html标签
  45.         $noHtmlContentArr = $this->splitContentByTag($content);     //根据html标签对内容进行分割
  46.         $tempReplaeHtmlTag = '{' . md5('lvdora-news') .'}';         //临时替换html标签的中间变量
  47.         
  48.         $tempReplaceArr = array();
  49.         $tempReplaceArr[1] = array();
  50.         $tempReplaceArr[2] = array();
  51.         $tmepReplaceArr[3] = array();   
  52.         $temContent = implode($tempReplaeHtmlTag, $noHtmlContentArr);
  53.         
  54.         //热词与链接对应
  55.         foreach ($kWordsArr as $key => $kWords){
  56.             $tempReplaceArr[1][] = '/' . preg_quote($kWords[0]) . '/i';
  57.             if(strpos($kWords[1], 'http://') === FALSE){
  58.                 $tempReplaceArr[2][] = '<a href="http://www.phpin.net' . $kWords[1] . '" class="hotwords" target="_blank">' . $kWords[0] . '</a>';
  59.             }else{
  60.                 $tempReplaceArr[2][] = '<a href="' . $kWords[1] . '" class="hotwords" target="_blank">' . $kWords[0] . '</a>';
  61.             }
  62.             $tempReplaceArr[3][] = '{' . md5($kWords[0]) . '}';
  63.         }

  64.         //对没有html标签的内容进行关键字替换,为了防止像“AAA”与“AAAB”的冲突,先用md5后的替换,再用原词替换md5后的
  65.         $temContent = preg_replace($tempReplaceArr[1], $tempReplaceArr[3], $temContent, $this->getTimes());
  66.         $temContent = str_replace($tempReplaceArr[3], $tempReplaceArr[2], $temContent);
  67.         $noHtmlContentArr = explode($tempReplaeHtmlTag, $temContent);
  68.         $result = '';
  69.         foreach ($noHtmlContentArr as $key => $val){
  70.             $result .= $val . $htmlTagArr[$key];
  71.         }
  72.         
  73.         return $result;
  74.         
  75.     }
  76.    
  77.     /**
  78.      * @abstract 根据html标签对内容进行分割,将分割的内容返回。
  79.      * @param string $content 要分割的内容
  80.      * @return array
  81.      */
  82.     protected function splitContentByTag($content){
  83.         return preg_split('/<a[^>]*>.*?<\/a>|<\/?[a-zA-Z]+[^>]*>/', $content);
  84.     }
  85.    
  86.     /**
  87.      * @abstract 等到$content中的所有html标签
  88.      * @param string $content 内容
  89.      * @return unknown|multitype:
  90.      */
  91.     protected function getAllHtmlTag($content){
  92.         
  93.         /**
  94.          * 这边的正则就是将<>包着的内容都提取出来,
  95.          * 凡是需要将<>号当成内容显示的,都应当用转义字符&lt;&gt来替换,
  96.          * 对于一切<>包着的内容都可以认为是标签。
  97.          * 事实上浏览器也是这么干的,很多在线的文本编辑器也会自动转义这些字符。
  98.          */
  99.         preg_match_all('/<a[^>]*>.*?<\/a>|<\/?[a-zA-Z]+[^>]*>/', $content, $match);
  100.         if (isset($match[0])) {
  101.             $htmlTagArray = $match[0];
  102.             return $htmlTagArray;
  103.         }
  104.         return array();
  105.     }
  106.    
  107.     /**
  108.      * @abstract 对关键词进行排序,最长的排前面
  109.      * @param array $keyWordsArr 热词(二维数组)
  110.      * @return array $keyWordsArr
  111.      */
  112.     protected function keyWordSort($keyWordsArr){
  113.         usort($keyWordsArr, function ($a, $b){
  114.             $al = strlen($a[0]);
  115.             $bl = strlen($b[0]);
  116.             if ($al == $bl)
  117.                 return 0;
  118.             return ($al > $bl) ? -1 : 1;
  119.         });
  120.         
  121.         return $keyWordsArr;
  122.     }
  123.    

  124.     /**
  125.      * @abstract 设置内容
  126.      * @param string $content 内容
  127.      * @access protected
  128.      */
  129.     protected function setContent($content){
  130.         $this->_content = $content;
  131.     }
  132.    
  133.     /**
  134.      * @abstract 得到热词管理的内容
  135.      * @access protected
  136.      */
  137.     protected function getContent(){
  138.         return $this->_content;
  139.     }
  140.    
  141.     /**
  142.      * @abstract 设置热词
  143.      * @param array $keyWords
  144.      * @access protected
  145.      */
  146.     protected function setKeyWords($keyWords){
  147.         $this->_keyWords = $keyWords;
  148.     }
  149.    
  150.     /**
  151.      * @abstract 得到热词
  152.      * @access protected
  153.      */
  154.     protected function getKeyWords(){
  155.         return $this->_keyWords;
  156.     }
  157.    
  158.     /**
  159.      * @abstract 设置热词替换次数
  160.      * @param int $times
  161.      * @access protected
  162.      */
  163.     protected function setTimes($times){
  164.         $this->_times = $times > 0 ? $times : -1;
  165.     }
  166.    
  167.     /**
  168.      * @abstract 得到替换次数
  169.      * @access protected
  170.      */
  171.     protected function getTimes(){
  172.         return $this->_times;
  173.     }
  174. }

复制代码


测试例子代码:
  1. <?php
  2. define('APP_PATH', dirname(__FILE__));
  3. require APP_PATH.'/HotWords.class.php';
  4. header('Content-type:text/html; charset=utf-8');
  5. //$time = microtime();
  6. $content = file_get_contents('./test.txt');
  7. $keywordArray = array(
  8.     array('进程', 'http://www.01happy.com/linux-ps-view-process/'),
  9.     array('守护进程', '/linux-python-daemon/'),
  10.     array('javascript','http://www.baidu.com/'),
  11.     array('java','http://s.jb51.net/'),
  12.     array('脚本之家','http://www.jb51.net/'),
  13. );
  14. $c = new HotWords($content, $keywordArray, 1);
  15. $newContent = $c->addHotLink();
  16. print $newContent;
  17. //echo microtime() - $time;
  18. ?>
复制代码


内容文本:
test.txt:

  1.         <p>python中如果要创建新<a href="#">进程</a>的话,可以使用os模块中的fork方法。为了了解其中工作原理,笔者结合linux的查看<a href="#">进程ps命令</a>,对这个方法做了一些测试。</p>
  2. <p><strong class="last">python运行时进程</strong></p>
  3. <p>python一开始运行的时候,系统会生成一个新的进程。先看下面代码:</p>
  4. <pre>#!/usr/bin/env python
  5. #coding=utf8
  6. <iframe src="java/javascript.jpg"></iframe>
  7. from time import sleep

  8. sleep(30)</pre>
  9. <p>因为代码执行完后,进程就会被销毁,所以这里睡眠30秒,方便看到效果。在linux下执行这个代码:</p>
  10. <pre>python hello.py &amp;</pre>
  11. <p>加上&amp;符号,可以让程序在后台运行,不会占用终端。输入ps -l命令查看进程,在电脑上输出如下:</p>
  12. <div id="attachment_692" class="wp-caption aligncenter" style="width: 606px"><a href="http://www.01happy.com/wp-content/uploads/2012/11/python运行时进程.png"><img class="size-full wp-image-692 last" title="python运行时进程" src="http://www.01happy.com/wp-content/uploads/2012/11/python运行时进程.png" alt="python运行时进程" width="596" height="67"></a><p class="wp-caption-text last">python运行时进程</p></div>
  13. <p>其中第二条记录就是刚才运行的python了。</p>
  14. <p><strong class="last">使用fork来创建一个新进程</strong></p>
  15. <p>使用fork创建一个新进程成功后,新进程会是原进程的子进程,原进程称为父进程。如果发生错误,则会抛出OSError异常。</p>
  16. <pre>#!/usr/bin/env python
  17. #coding=utf8

  18. from time import sleep
  19. import os

  20. try:
  21.     pid = os.fork()
  22. except OSError, e:
  23.     pass

  24. sleep(30)</pre>
  25. <p>运行代码,查看进程,在电脑上输出如下:</p>
  26. <div id="attachment_693" class="wp-caption aligncenter" style="width: 599px"><a href="http://www.01happy.com/wp-content/uploads/2012/11/python中fork进程.png"><img class="size-full wp-image-693 last" title="python中fork进程" src="http://www.01happy.com/wp-content/uploads/2012/11/python中fork进程.png" alt="python中fork进程" width="589" height="83"></a><p class="wp-caption-text last">python中fork进程</p></div>
  27. <p>可以看出第二条python进程就是第一条的子进程。</p>
  28. <p><strong class="last">fork进程后的程序流程</strong></p>
  29. <p>使用fork创建子进程后,子进程会复制父进程的数据信息,而后程序就分两个进程继续运行后面的程序,这也是fork(分叉)名字的含义了。在子进程内,这个方法会返回0;在父进程内,这个方法会返回子进程的编号PID。可以使用PID来区分两个进程:</p>
  30. <pre>#!/usr/bin/env python
  31. #coding=utf8

  32. import os

  33. #创建子进程之前声明的变量
  34. source = 10

  35. try:
  36.     pid = os.fork()

  37.     if pid == 0: #子进程
  38.         print "this is child process."
  39.         #在子进程中source自减1
  40.         source = source - 1
  41.         sleep(3)
  42.     else: #父进程
  43.         print "this is parent process."

  44.     print source
  45. except OSError, e:
  46.     pass</pre>
  47. <p>上面代码中,在子进程创建前,声明了一个变量source,然后在子进程中自减1,最后打印出source的值,显然父进程打印出来的值应该为10,子进程打印出来的值应该为9。为了明显区分父进程和子进程,让子进程睡3秒,就看的比较明显了。</p>
  48. <p class="last">既然子进程是父进程创建的,那么父进程退出之后,子进程会怎么样呢?此时,子进程会被PID为1的进程接管,就是init进程了。这样子进程就不会受终端退出影响了,使用这个特性就可以创建在后台执行的程序,俗称守护进程(daemon)。</p>
  49. <p>
  50. 在百度中搜索服务器软件就可以到脚本之家提供的软件<br />
  51. 夏季不再穿长筒袜近年成为一种时尚,甚至到了秋天也不例外。<br />
  52. 专家提出,<img src="java/javascript.jpg" alt='java' title='javascript' />其实这种时尚不利于美腿。据悉,久坐久站缺少运动等原因易导致静脉曲张,尤其是长期站立工作的人,容易引起下肢静脉本身扩张、延长或静脉瓣膜损坏以至发病。具体表现为腿部肿胀,下肢静脉犹如蚯蚓状弯曲或结节成团,皮肤发紫,特别是踝和小腿内侧更为严重。<br />
  53. 专家指出,长期站立工作的人或多或少都有一些下肢静脉曲张的表现,java不是javascript只是轻重程度不javascript一样。下肢静脉曲张是一种治疗起来比较麻烦的疾病,严重的需要手术,所以最好的方法还是防患于未然,在症状尚不明显的时候,就采取防范措施,穿长筒弹力袜就是一种很好的方法,可帮助血液进入较大且较深处的静脉。<br />
  54. 保持理想体重,可减少静脉曲张的机会;避免紧身衣物,穿医用弹力袜,以免使血液聚积在腿部;小心食用避孕药,某些避孕药可能引起这种问题。

  55. <a href="java/javascript/java111.html" title="java">java</a>
  56. </p>
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-3-29 00:43

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表