正则表达式帮助 - 我怎样才能使某些网址不被关注?
好的,我已经制作了这个函数,可以很好地将大多数 url(如 pies.com 或 www.cakes.com)转换为实际的链接标记。
function render_hyperlinks($str){
$regex = '/(http:\/\/)?(www\.)?([a-zA-Z0-9\-_\.]+\.(com|co\.uk|org(\.uk)?|tv|biz|me)(\/[a-zA-Z0-9\-\._\?&=#\+;]+)*)/ie';
$str = preg_replace($regex,"'<a href=\"http://www.'.'$3'.'\" target=\"_blank\">'.strtolower('$3').'</a>'", $str);
return $str;
}
我想更新此功能以添加 no-follow
标签来链接到我的竞争对手,
这样我就会有某些关键字(竞争对手名称)为 nofollow,例如,如果我的网站是关于烘焙的,我可能想要:
no-follow any sites with the phrases 'bakingbrothers', 'mrkipling', 'lyonscakes'
是否可以将这个 if(contains x){ add y}
实现到我的正则表达式中?
这就是所谓的“回顾”吗?
Ok so i've made this function which works fine for converting most urls like pies.com or www.cakes.com to an actual link tag.
function render_hyperlinks($str){
$regex = '/(http:\/\/)?(www\.)?([a-zA-Z0-9\-_\.]+\.(com|co\.uk|org(\.uk)?|tv|biz|me)(\/[a-zA-Z0-9\-\._\?&=#\+;]+)*)/ie';
$str = preg_replace($regex,"'<a href=\"http://www.'.'$3'.'\" target=\"_blank\">'.strtolower('$3').'</a>'", $str);
return $str;
}
I would like to update this function to add no-follow
tags to links to my competitors,
so i would have certain keywords (competitor names) to nofollow for example if my site was about baking i might want to:
no-follow any sites with the phrases 'bakingbrothers', 'mrkipling', 'lyonscakes'
is it possible to implement this if(contains x){ add y}
into my regex?
is this what is called a 'lookback'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许 preg_replace_callback 就是您正在寻找的:
Maybe preg_replace_callback is what you are looking for: