如果没有使用PHP的标题标签,则添加一个nofollow属性来链接 [英] Add a nofollow attribute to link if no title tag present using PHP
本文介绍了如果没有使用PHP的标题标签,则添加一个nofollow属性来链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如果链接如下所示:
< a href =test.html> test< /一个>
我希望它看起来像:
< a rel =nofollowhref =test.html> test< / a>
但如果链接如下所示:
< a title =test titlehref =test.html> test< / a>
我不想添加rel =nofollow属性。如何在php中执行此操作?
编辑:
没有提到这个,但我正在使用PHP4。是的,我知道,但我坚持使用PHP4。
解决方案
首先使用preg匹配来获取标题是否被添加。
$ str ='< a href =test.html> test< / a>';
if(!preg_match('/ title = /',$ str))
{
$ str = str_replace('href =','rel =nofollowhref =',$ STR);
}
I have a bunch of text with html in it. Basically what I want to do is for all links found in this text I want to add a rel="noindex" to every link found only if the title attribute is no present.
For example if a link looks like this:
<a href="test.html">test</a>
I want it to look like:
<a rel="nofollow" href="test.html">test</a>
But if the link looks like this:
<a title="test title" href="test.html">test</a>
I dont want to add the rel="nofollow" attribute to that. How can I do that in php?
EDIT:
Im sorry I didnt mention this but I am using PHP4. Yes I know but Im stuck with PHP4.
解决方案
First use preg match to get if title is added.
$str = '<a href="test.html">test</a>';
if(!preg_match('/title=/', $str))
{
$str = str_replace('href=', 'rel="nofollow" href=', $str);
}
这篇关于如果没有使用PHP的标题标签,则添加一个nofollow属性来链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文