在 PHP 中将纯文本 URL 转换为 HTML 超链接
我有一个简单的评论系统,人们可以在纯文本字段内提交超链接。当我将这些记录从数据库显示回网页并显示到网页中时,我可以使用 PHP 中的哪些 RegExp 将这些链接转换为 HTML 类型的锚链接?
我不希望算法对任何其他类型的链接执行此操作,仅使用 http 和 https。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这是另一个解决方案,这将捕获所有 http/https/www 并转换为可点击的链接。
或者,如果只是捕获 http/https,则使用下面的代码。
编辑:
下面的脚本将捕获所有 URL 类型并将它们转换为可点击的链接。
新的更新,如果您让字符串去掉 (s),则使用下面的代码块,感谢 @AndrewEllis 指出了这一点。
这是解决 URL 无法正确显示的一个非常简单的解决方案。
这是一个非常简单的修复,但您必须根据自己的目的对其进行修改。
我提供了多个答案,因为某些服务器的设置不同,因此一个答案可能适用于某些服务器,但不适用于其他服务器,但我希望答案对您有用,如果没有,请告诉我,希望我可以想出另一个解决方案。
有多个脚本,因为有些 PHP 文件需要不同的脚本,有些服务器的设置也不同,而且每个脚本都有不同的要求,有些只需要 HTTP/S,有些需要 WWW,有些需要 FTP/S,每个脚本的工作方式取决于设置了用户自己的脚本,我为每个脚本提供了一些文本以及他们所做的事情。
Here is another solution, This will catch all http/https/www and convert to clickable links.
Alternatively for just catching http/https then use the code below.
EDIT:
The script below will catch all URL types and convert them to clickable links.
The new update, If you're having the string strip the (s) then use the below code block, Thanks to @AndrewEllis for pointing this out.
Here's a very simple solution for the URL not displaying correctly.
It is a very simple fix but you will have to modify it for your own purpose.
I've provided multiple answers as some servers are set up differently, so one answer may work for some but not for others, but I hope the answer(s) work for you and if not then let me know, and hopefully, I can come up with another solution.
There are multiple scripts as some PHP files require different scripts also some servers are set up differently, Plus each has different requirements, Some want just HTTP/S, some want WWW and some want FTP/S, Each one will work depending on how the users own scripts are set up, I provided some text with each one with what they do.
好吧,沃洛米克的答案更接近。为了更进一步,我做了以下操作,忽略了超链接末尾的尾随句点。我还考虑了 URI 片段。
Well, Volomike's answer is much closer. And to push it a bit further, here's what I did for it to disregard the trailing period at the end of the hyperlinks. I also considered URI fragments.
请参阅http://zenverse.net/php-function-to-auto-convert- url-into-hyperlink/。
这就是wordpress解决这个问题的方法
Refer http://zenverse.net/php-function-to-auto-convert-url-into-hyperlink/.
This is how wordpress solve it
评分最高的答案没有为我完成这项工作,以下链接未正确替换:
http://www.fifa.com/worldcup/matches/round255951/match=300186487/index.html#nosticky
经过一些谷歌搜索和一些测试,这就是我想到的与:
我不是正则表达式方面的专家,实际上它让我很困惑:)
所以请随意发表评论并改进这个解决方案。
The most rated answer didn't do the job for me, following link was not replaced correctly:
http://www.fifa.com/worldcup/matches/round255951/match=300186487/index.html#nosticky
After some google searches and some tests, this is what I came up with:
I'm not an expert in regex, actually it quite confuses me :)
So feel free to comment and improve this solution.
这是我的代码,用于格式化文本内的所有链接,包括电子邮件、带协议和不带协议的网址。
请评论无效的网址。我会尝试更新正则表达式。
Here is my code to format all the links inside text, including emails, urls with and without protocol.
Please comment the url that doesn't work. I'll try to update the regex.
MkVal 的答案有效,但在我们已经有了锚链接的情况下,它将以奇怪的格式呈现文本。
这是在这两种情况下都适合我的解决方案:
The answer from MkVal works but in the case we already have the anchor link, it will render the text in weird format.
Here is the solution which works for me in both cases:
试试这个:
它会跳过现有链接(如果我们已经有一个 href,它不会在 href 中添加一个 href)。否则,它将添加带有空白目标的a href。
Try this one:
It skips the existing links (if we already have a href, it won't add a href inside of a href). Otherwise it will add the a href with blank target.
不是我的代码,我从这里得到它 https://gist.github.com/jasny/2000705
Not my code, I got it from here https://gist.github.com/jasny/2000705
我建议不要像这样在飞行中做很多事情。我更喜欢使用简单的编辑器界面,例如 stackoverflow 中使用的编辑器界面。它称为 Markdown。
I recommend not to do many things on fly like this. I prefer to use simple editor interface like the one used in stackoverflow. It is called Markdown.
我正在使用源自 question2answer 的函数,它接受纯文本,甚至 html 中的纯文本链接:
由于接受包含括号和其他字符的链接,代码有点多,但可能有帮助。
I am using a function that originated from question2answer, it accepts plain text and even plain text links in html:
A bit much code due to accepting links that hold brackets and other characters, but probably it helps.
在 HTML 中查找纯文本链接
我真的很喜欢 这个答案 - 但我需要一个解决方案来处理非常内部可能的纯文本链接简单的 HTML 文本:
这意味着我需要正则表达式模式来忽略 html 字符
<
和>
正则表达式调整
所以我更改了部分模式为
[^\s\>\<]
而不是\S
\S
- 不是空格;匹配任何非空白字符(制表符、空格、换行符)[^]
- 否定集;匹配不在集合中的任何字符我的答案中的函数版本
我需要除 HTML 之外的另一种格式,因此我将正则表达式与其替换项分开以适应这一点。
我还添加了一种仅返回在数组中找到的链接/电子邮件的方法,这样我就可以将它们作为关系保存在我的帖子中(非常适合稍后为它们制作元卡......以及分析!) )。
更新:连续的句点匹配
我得到了类似
there...it
的文本匹配 - 所以我想确保我没有得到任何包含连续点的匹配。注意:为了解决这个问题,我添加了一个额外的格式字符串来撤消匹配它们,以避免必须重做这些可靠的 url 正则表达式。
Finding Plain Text Links inside HTML
I really liked this answer - yet I needed a solution for possible plain text links that are inside very simple HTML text:
This meant I needed the regex patterns to ignore the html chars
<
and>
Regex Adjustment
So I changed parts of the patterns to
[^\s\>\<]
instead of\S
\S
- not white-space; matches any char that is not white-space (tab, space, newline)[^]
- a negated set; matches any char not in the setMy version of the function from this answer
I needed another format in addition to HTML so I separated out the regexes from their replacements to accommodate this.
I also added a way to return just the links/emails found into an array so I can save them as a relationship on my posts (great for making meta cards for them later ...and for analytics!).
UPDATE: Consecutive periods were matching
I was getting matches for text like
there...it
- So I wanted to ensure I didn't get any matches that included consecutive dots.Note: To accomplish fixing this, I added an additional format string to undo matching them to avoid having to redo these otherwise reliable url regexes.
结果:
我喜欢这个解决方案的是,它还将
www.example.com
转换为http://www.example.com
因为不起作用(如果没有
http/https
协议,它会指向yourdomain.com/www.example。 com
)。Result:
What I like in this solution is that it also converts
www.example.com
tohttp://www.example.com
because<a href="www.example.com"></a>
doesn't work (withouthttp/https
protocol it points toyourdomain.com/www.example.com
).如果我是对的,你要做的就是将普通文本转换为http链接。我认为以下内容可以提供帮助:
If am right, what you want to do is turn ordinary text into http links. Here's what I think can help: