php开发文章内链,替换规则的一个有关问题

WBOY
Release: 2016-06-13 12:56:03
Original
910 people have browsed it

php开发文章内链,替换规则的一个问题
做内链的方法,能想到的就是替换 设定好的内链词,比如把 “春节放假” 替换成 “春节放假”,等,这种方式来的

但是事实可能并没有这么简单,因为发过来的文章往往都是带有html标签的,比如可能是“春节放假”这种形式的,这样改怎么样来替换?

想了下可能有下边的集中可能

“春节放假” 这种是要替换的  ,成这样 “春节放假” 

"春节放假" 这种直接形式的不替换

春节放假” 这种隔标签形式的也不替换

春节放假” 这种部分词语有链接的不替换

。。。可能还有替他的情况

用语言描述下,“每一个分词都不带链接的情况下才替换,链接可能是直接链接也可能是隔标签链接”

然后替换成 “多个分词共同的最近父节点内部,替换成新加好链接的新形式”。

这种匹配规则和替换规则还咋写都?求助

php seo 内链
------解决方案--------------------
你想的太复杂了,被html标记包裹的就不需要替换
因为标记可能已经被赋予特定意义,比如a标记就是连接
虽然你想的很复杂,但还有一处没考虑到(标记内的):
春节放假'>春节放假
示例
$html = <<<html<br />
这儿是茶叶的链接。<br />
<img src="中国好茶叶.jpg" width="120"    style="max-width:90%" alt="中国好茶叶" /><br />
<span title="中国好茶叶">中国茶叶</span><br />
这儿是中国茶叶大观的链接。<br />
这儿是<a href="原有的链接.html">茶叶</a>的现有链接<br />
html;<br />
echo preg_replace('#(?=[^>]*(?=<(?!/a>)<br><font color='#FF8000'>------解决方案--------------------</font><br>$))茶叶#','<a href="新加的链接.html">\0</a>',$html);
Copy after login
得到

这儿是茶叶的链接。
 php开发文章内链,替换规则的一个有关问题
中国茶叶
这儿是中国茶叶大观的链接。
这儿是茶叶的现有链接
Related labels:
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!