1 <?php
2 //PHP实现给文章关键词加链接功能
3 header("Content-Type:text/html;charset=utf-8");
4 $content = 'PHP数组,怎么找出最大值与关键字1最小值,和它们所对应的数组下标';
5 $keywordArray = array(
6 array('关键字1', 'https://www.xxx.com/url3/'),
7 array('关键字2', 'https://www.xxx.com/url1/')
8 );
9
10 //调用方法
11 $c = new Content($content, $keywordArray);
12 $newContent = $c->addKeywordLink();
13 print $newContent;
14
15
16 //相关函数
17 class Content {
18
19 /**
20 * 文本
21 *
22 * @var string
23 */
24 protected $_content;
25
26 /**
27 * 关键词
28 *
29 * @var string
30 */
31 protected $_keyword;
32
33 /**
34 * 构造方法
35 *
36 * @param string $content
37 * @param string $keyword
38 */
39 public function __construct($content, $keyword) {
40 $this->setContent($content);
41 $this->setKeyword($keyword);
42 }
43
44 /**
45 * 给关键词加上链接
46 *
47 * @return string
48 */
49 public function addKeywordLink() {
50 $content = $this->getContent();
51 $keywordArray = $this->sortKeywordArray($this->getKeyword());
52
53 $htmlTagArray = $this->getAllHtmlTag($content);
54 $noTagContentArray = $this->splitContentByHtmlTag($content);
55
56 /**
57 这边定义了一个临时数组
58 $tempReplaceArray[1]用来存储所有关键词的正则表达式
59 $tempReplaceArray[2]用来存储关键词的md5值
60 $tempReplaceArray[3]用来存储需要替换的带链接的关键词
61 */
62 $tempReplaceArray = array();
63 $tempReplaceArray[1] = array();
64 $tempReplaceArray[2] = array();
65 $tempReplaceArray[3] = array();
66
67 foreach ($keywordArray as $keyword) {
68 $tempReplaceArray[1][] = '/' . preg_quote($keyword[0]) . '/i';
69 $tempReplaceArray[2][] = '{' . md5($keyword[0]) . '}';
70 $tempReplaceArray[3][] = '<a href="' . $keyword[1] . '" target="_blank">' . $keyword[0] . '</a>';
71 }
72
73 //循环替换每一段内容
74 foreach ($noTagContentArray as $key => $noTagIntro) {
75 //如果是空内容,则直接进行下一步
76 if (!trim($noTagIntro)) {
77 continue;
78 }
79
80 $noTagIntro = preg_replace($tempReplaceArray[1], $tempReplaceArray[2], $noTagIntro,1); //设置只替换一次
81 $noTagIntro = str_replace($tempReplaceArray[2], $tempReplaceArray[3], $noTagIntro);
82 $noTagContentArray[$key] = $noTagIntro;
83 }
84
85 //组合提取的html标签和处理后的内容
86 $finalContentArray = array();
87
88 $count = count($noTagContentArray);
89 for ($i = 0; $i < $count; $i++) {
90 $finalContentArray[] = $noTagContentArray[$i];
91 if (isset($htmlTagArray[$i])) {
92 $finalContentArray[] = $htmlTagArray[$i];
93 }
94 }
95
96 return implode('', $finalContentArray);
97 }
98
99 /**
100 * 对关键词进行排序,最长的排前面
101 * @param array $keywordArray
102 * @return array $keywordArray
103 */
104 public function sortKeywordArray($keywordArray) {
105 usort($keywordArray, function($a, $b) {
106 $al = strlen($a[0]);
107 $bl = strlen($b[0]);
108 if ($al == $bl)
109 return 0;
110 return ($al > $bl) ? -1 : 1;
111 });
112 return $keywordArray;
113 }
114
115 /**
116 * 提取出所有html标签
117 *
118 * @param string $content
119 * @return array
120 */
121 public function getAllHtmlTag($content) {
122 preg_match_all('/<\/?[a-zA-Z]+[^>]*>/', $content, $match);
123 if (isset($match[0])) {
124 $htmlTagArray = $match[0];
125 return $htmlTagArray;
126 }
127 return array();
128 }
129
130 /**
131 * 根据html标签切割内容
132 *
133 * @param string $content
134 * @return array
135 */
136 public function splitContentByHtmlTag($content) {
137 $noTagContentArray = preg_split('/<\/?[a-zA-Z]+[^>]*>/', $content);
138 return $noTagContentArray;
139 }
140
141 /**
142 * 设置关键词
143 *
144 * @param string $keyword
145 */
146 public function setKeyword($keyword) {
147 $this->_keyword = $keyword;
148 }
149
150 /**
151 * 获取关键词
152 *
153 * @return string
154 */
155 public function getKeyword() {
156 return $this->_keyword;
157 }
158
159 /**
160 * 设置内容
161 *
162 * @param string $content
163 */
164 public function setContent($content) {
165 $this->_content = $content;
166 }
167
168 /**
169 * 获取内容
170 *
171 * @return string
172 */
173 public function getContent() {
174 return $this->_content;
175 }
176
177 }