什么是php代码来附加class=“外部”到发布的链接,而不是域名。
例如,我的网站是新闻,您发布了一个指向www.mysite.com /www.mysite.com的链接和一个指向www.yoursite.com的链接
如何将其设置为只有非"mysite.com“链接才有指定的类?
发布于 2010-04-08 22:16:12
另一种方法是使用CSS3选择器,但Internet Explorer6不支持它们(参见:http://www.webdevout.net/browser-support-css#css3selectors):
// external link style
a:link[href^="http://"] {
background-color: #990000;
}
// internal link style
a:link, a:link[href^="http://www.mydomain.com"] {
background-color: #009900;
color: #000000;
}您不需要以编程方式更改您的链接!
<a href="somelink.htm">some link</a>
<a href="http://www.mydomain.com/anotherlink.htm">another link</a>..。将导致具有内部链接样式的链接。
<a href="http://www.otherdomain.com">external link</a>..。将导致具有外部链接样式的链接。
发布于 2010-04-08 21:54:27
一种方法是检查"http://“(或"https://")”的URL内容。
事实上,还要检查它是否包含您的域名(以防您有一个完全合格的内部链接)。
在PHP中,使用strpos()
也可以在客户端使用相同的通用技术进行处理。(JQuery是一个显而易见的选择,因为它涉及到添加一个类。)
发布于 2010-04-08 22:40:19
不要用正则表达式解析html。真的。RegEx match open tags except XHTML self-contained tags
使用如下的DOM解析器:http://simplehtmldom.sourceforge.net/
$html = $xxxx //load it from your db or wherever it is
$dom = new simple_html_dom();
$dom->load($html);
foreach($dom->find('a') as $a) {
$a->class = 'external';
}完成了!
https://stackoverflow.com/questions/2600357
复制相似问题