检测URL并在其周围添加achor-tags [英] Detect URL and add achor-tags around it

查看:51
本文介绍了检测URL并在其周围添加achor-tags的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开发了一个使用PHP的邮件列表系统。现在,我想确保如果有人输入一个URL,PHP脚本会检测它并在其周围添加锚标签。因此,如果我输入 http://www.example.com ,则会将其更改为< a href =http://www.example .COM> HTTP://www.example.com< / A> 。显示的文本不仅仅是URL,主要的问题是添加锚标签。

有没有简单的方法来解决这个问题?

解决方案

这是一个稍微好一点的版本:

  $ link = preg_replace(/(http:\ / \ / [^ \s] + \。+ [a-zAZ] {2,6})/ ,< a href = \$ 1 \> $ 1< / a>,$ link); 

这个检查链接和域名是否有点(如.com .org。 nl)

Recently I've developed a mailinglistsystem with PHP. Now, I'd like to make sure that if someone types a URL, the PHP-script detects it and adds the anchor-tags around it. So if I type http://www.example.com, this is changed into <a href="http://www.example.com">http://www.example.com</a>. It doesn't matter that the displayed text is just the URL, the main issue is just adding the anchor-tags.

Is there a simple way to fix this? Thanks in advance!

解决方案

here is a little bit better version:

$link = preg_replace("/(http:\/\/[^\s]+\.+[a-zAZ]{2,6})/","<a href=\"$1\">$1</a>", $link);

This one checks if there is a dot in the link and a domain (like .com .org .nl)

这篇关于检测URL并在其周围添加achor-tags的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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