【发布时间】:2016-01-12 06:31:07
【问题描述】:
我需要将纯文本链接转换为 HTML 链接的正则表达式。 我的代码是:
preg_replace('/(((f|ht){1}tps:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'<a href="\\1" target="_blank">\\1</a>', $text);
但是这个表达式也会使图像 url 成为 href。 所以我的问题是如果网址在 img 标签中,如何避免。
正文:
https://yahoo.com this is my image <img src="https://img.com/img.jpg">
结果与我的表情:<a href="https://yahoo.com">https://yahoo.com</a> this is my image <img src="<a href="https://img.com/img.jpg">https://img.com/img.jpg</a>">
我想要这个<a href="https://yahoo.com">https://yahoo.com</a> this is my image <img src="http://img.com/img.jpg">
【问题讨论】:
-
您应该在每个示例之后显示您拥有的确切纯文本,然后是预期的转换输出,这会更容易提供帮助
-
我为你标记了
php,因为你使用的是preg_replace(),所以regex可能很重要