对于互联网程序来说,对文字正文内容做关键词内链优化是常态的工作之一。一方面有人手动来处理关键词内链,这个效率太低;一方面通过程序自动添加内链,这样子也省事而且便于管理; 今天我们探讨的就是给自动给文章增加内链的方法。 文章内链: 铁牛 http://
对于互联网程序来说,对文字正文内容做关键词内链优化是常态的工作之一。一方面有人手动来处理关键词内链,这个效率太低;一方面通过程序自动添加内链,这样子也省事而且便于管理;
今天我们探讨的就是给自动给文章增加内链的方法。
文章内链:
铁牛 http://www.111.com
李铁牛 http://www.222.com
李铁牛是铁牛 http://www.333.com
李铁牛是铁牛山的铁牛 http://www.444.com
文字内链存储的数组
<span>$keywordArr</span>=<span>array</span><span> ( </span>0 => <span>array</span> ('title' => '李铁牛是铁牛山的铁牛','url' => 'http://www.444.com',), 1 => <span>array</span> ('title' => '李铁牛是铁牛山','url' => 'http://www.333.com',), 2 => <span>array</span> ('title' => '李铁牛','url' => 'http://www.222.com',), 3 => <span>array</span> ('title' => '铁牛','url' => 'http://www.111.com',),);
文章正文:
这是我下面对内链关键词的举例哈。铁牛爱美女,李铁牛喜欢有情调的生活。铁牛也是牛,铁牛喜欢喝酒喜欢妹妹喜欢打拳健身,李铁牛也是牛,铁牛山的铁牛是李铁牛,李铁牛是铁牛山的铁牛。牛牛牛。
替换过的正文html
这是我下面对内链关键词的举例哈。<span><span>a </span><span>href</span><span>="http://www.111.com"</span><span> target</span><span>="_blank"</span><span>><span>strong </span><span>class</span><span>="keylink"</span><span>></span>铁牛<span></span><span>strong</span><span>></span><span>a</span><span>></span>爱美女,<span><span>a </span><span>href</span><span>="http://www.222.com"</span><span> target</span><span>="_blank"</span><span>><span>strong </span><span>class</span><span>="keylink"</span><span>></span>李铁牛<span></span><span>strong</span><span>></span><span>a</span><span>></span>喜欢有情调的生活。<span><span>a </span><span>href</span><span>="http://www.111.com"</span><span> target</span><span>="_blank"</span><span>><span>strong </span><span>class</span><span>="keylink"</span><span>></span>铁牛<span></span><span>strong</span><span>></span><span>a</span><span>></span>也是牛,铁牛喜欢喝酒喜欢妹妹喜欢打拳健身,<span><span>a </span><span>href</span><span>="http://www.222.com"</span><span> target</span><span>="_blank"</span><span>><span>strong </span><span>class</span><span>="keylink"</span><span>></span>李铁牛<span></span><span>strong</span><span>></span><span>a</span><span>></span>也是牛,铁牛山的铁牛是李铁牛,<span><span>a </span><span>href</span><span>="http://www.444.com"</span><span> target</span><span>="_blank"</span><span>><span>strong </span><span>class</span><span>="keylink"</span><span>></span>李铁牛是铁牛山的铁牛<span></span><span>strong</span><span>></span><span>a</span><span>></span>。牛牛牛。</span></span></span></span></span></span></span></span></span></span>
那么这种长尾短尾想结合的关键词我们用常规的替换处理是不行的,需要用到正则的匹配处理。处理函数见下
<span>//</span><span>content 文字正文内容</span> <span>function</span> keylink(<span>$content</span><span>) { </span><span>//</span><span>$keywordArr 代表我们的关键词字典</span> <span>$keywordArr</span>=<span>array</span><span> ( </span>0 => <span>array</span> ('title' => '李铁牛是铁牛山的铁牛','url' => 'http://www.444.com',), 1 => <span>array</span> ('title' => '李铁牛是铁牛山','url' => 'http://www.333.com',), 2 => <span>array</span> ('title' => '李铁牛','url' => 'http://www.222.com',), 3 => <span>array</span> ('title' => '铁牛','url' => 'http://www.111.com',),<span>); </span><span>if</span>(!<span>$keywordArr</span>) <span>return</span> <span>$content</span><span>; </span><span>$data</span> = <span>$content</span><span>; </span><span>foreach</span>(<span>$keywordArr</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) { </span><span>$quote</span> = <span>str_replace</span>(<span>array</span>("'", '-'), <span>array</span>("\'", '\-'), <span>preg_quote</span>(<span>$v</span>['title'<span>])); </span><span>$data</span> = <span>preg_replace</span>('\'(?!((<.>$quote.')(?!(([^]*?)>)|([^>]*?)|([^>]*?))\'si', '<a href="'.<span>%24v</span>%5B'url'%5D.'" target="_blank"><strong class="keylink">'.<span>$v</span>['title'].'</strong></a>', <span>$data</span>, 1);<span>//</span><span>数字1代表在正则中出现的替换频率</span> <span>if</span>(<span>$data</span> == '') <span>$data</span> = <span>$content</span><span>; } </span><span>return</span> <span>$data</span><span>; }</span></.>
如果处理的不好,会出现内链嵌套内链的错误悲剧。错误悲剧出处:大家可以尝试用下DEDECMS,DEDECMS的内链就有短尾关键词和长尾关键词嵌套一起的错误内链问题。