微调正则表达式以跳过标签 [英] Finetune Regex to skip tags

查看:70
本文介绍了微调正则表达式以跳过标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码必须跳过现有链接, 的 src 值(或类似的东西)

Code must skip existing links, <img>'s src values (or something like that)

public function convertUrlsToLinks($text){
    return preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $text);
}

想不通,这个函数需要修改什么?

Can't figure out, what I need to modify in this function?

推荐答案

向您的正则表达式添加一小部分,以检查您的正则表达式是否不在标签内部".所以应该是

Add a small part to your regex that checks if your regex isn't 'inside' a tag. So it should be

     <tag>HERE</tag> 

而不是

     <tag src="HERE"></tag>.

由于 url 将始终位于 html 标记的内部,如果您将正则表达式微调到此用途,它应该可以工作.我不是正则表达式的专业人士,所以不能帮助你更多,但我认为这应该让你朝着正确的方向前进.

Since a url will always be INSIDE the html tag it should work if you finetune your regex to this use. I'm not a pro with regex' so can't help you more than this, but I think this should get you in the right direction.

这篇关于微调正则表达式以跳过标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆