在 PHP 中将纯文本 URL 转换为 HTML 超链接
有没有办法在 PHP 中将纯文本 URL 转换为 HTML 超链接?我需要转换以下类型的 URL:
http://example.com
http://example.org
http://example.gov/
http://example.com?q=sometext&opt=thisandthat
http://example.com#path
http://example.com?q=querypath#path
http://example.com/somepath/index.html
https://example.com/
https://example.org/
https://example.gov/
https://example.com?q=sometext&opt=thisandthat
https://example.com#path
https://example.com?q=querypath#path
https://example.com/somepath/index.html
http://www.example.com/
http://www.example.org/
http://www.example.gov/
http://www.example.com?q=sometext&opt=thisandthat
http://www.example.com#path
http://www.example.com?q=querypath#path
http://www.example.com/somepath/index.html
https://www.example.com/
https://www.example.org/
https://www.example.gov/
https://www.example.com?q=sometext&opt=thisandthat
https://www.example.com#path
https://www.example.com?q=querypath#path
https://www.example.com/somepath/index.html
www.example.com/
www.example.org/
www.example.gov/
www.example.com?q=sometext&opt=thisandthat
www.example.com/#path
www.example.com?q=querypath#path
www.example.com/somepath/index.html
example.com/
example.org/
example.gov/
example.com?q=sometext&opt=thisandthat
example.com/#path
example.com?q=querypath#path
example.com/somepath/index.html
Is there anyway to convert plain text URLs to HTML hyperlinks in PHP? I need to convert URLs of following type:
http://example.com
http://example.org
http://example.gov/
http://example.com?q=sometext&opt=thisandthat
http://example.com#path
http://example.com?q=querypath#path
http://example.com/somepath/index.html
https://example.com/
https://example.org/
https://example.gov/
https://example.com?q=sometext&opt=thisandthat
https://example.com#path
https://example.com?q=querypath#path
https://example.com/somepath/index.html
http://www.example.com/
http://www.example.org/
http://www.example.gov/
http://www.example.com?q=sometext&opt=thisandthat
http://www.example.com#path
http://www.example.com?q=querypath#path
http://www.example.com/somepath/index.html
https://www.example.com/
https://www.example.org/
https://www.example.gov/
https://www.example.com?q=sometext&opt=thisandthat
https://www.example.com#path
https://www.example.com?q=querypath#path
https://www.example.com/somepath/index.html
www.example.com/
www.example.org/
www.example.gov/
www.example.com?q=sometext&opt=thisandthat
www.example.com/#path
www.example.com?q=querypath#path
www.example.com/somepath/index.html
example.com/
example.org/
example.gov/
example.com?q=sometext&opt=thisandthat
example.com/#path
example.com?q=querypath#path
example.com/somepath/index.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如何通过正则表达式运行它们,如下所示,用于重新激活 Twitter 链接 -
http://www.burnmind.com/howto/how-to-convert-twitter-mentions-and-urls-to-活跃链接使用正则表达式在 php
How about running them through a regular expression like the following used to re-activate Twitter links -
http://www.burnmind.com/howto/how-to-convert-twitter-mentions-and-urls-to-active-links-using-regular-expressions-in-php
如果您只需要转换单个 URL,则非常简单:
对于出现在较大文本块中的 URL,例如用户评论,请参阅此问题:
If you just need to convert a single URL, it's quite easy:
For URLs appearing in a larger text block, e.g. user comments, see this question:
试试这个功能。如果文本以 http 或 www 开头,这将创建链接。 example.com 将无法工作。
Try this function. this will make link if text start with http or www. example.com will not work.
呃,将它们包裹在锚标签中?
Er, wrap them in an anchor tag?
这就是我在我制作的一个小论坛上用来做同样事情的方法。一般来说,我会通过它运行整个评论,例如
echo makeLinks($forumpost['comment']);
This is what I use to do the same thing on a small forum I made. Generally I run the whole comment through it like
echo makeLinks($forumpost['comment']);
这是我的做法(包括测试用例):
测试:
Here is how I did it (with test cases included):
Tests:
如果最终用户不包含“http://”或“https://”(在我的例子中将链接复制到表单中)。
基于我上面 Ryan 的代码:
In case the end user doesn't include 'http://' or 'https://' (in my case copying a link into a form).
Based on Ryan's code above me: