如何使用 preg_replace 生成正确的 URL
我正在尝试找到 preg_replace 的模式,它将匹配段落中格式不正确的 HTML 链接/URL,并将其替换为正确的链接。我真的不知道如何开始。
这就是我需要形成 URL 的方式(需要包含 http://):
<a href="http://www.example.com" target="_blank">www.example.com</a>
错误的 URL 很可能以以下形式出现:
example.com
www.example.co.uk
<a href="example.com">example.com</a>
<a href=example.com>example.com</a>
任何帮助或建议将不胜感激。
谢谢,
eb_dev
I'm trying to find a pattern for preg_replace which will match incorrectly formed HTML links / URLs in a paragraph and replace them with correct links. I'm really not sure how to start with it.
This is how I need the URL to be formed (http:// needs to be included):
<a href="http://www.example.com" target="_blank">www.example.com</a>
Bad URLs may well come in the form of:
example.com
www.example.co.uk
<a href="example.com">example.com</a>
<a href=example.com>example.com</a>
Any help or advise would be greatly appreciated.
Thanks,
eb_dev
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
好的,找到了一个似乎效果很好的解决方案:
Ok found a solution that seems to work pretty well: