URL的PHP​​验证/正则表达式 [英] PHP validation/regex for URL

查看:76
本文介绍了URL的PHP​​验证/正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找URL的简单正则表达式,有人能用得起吗?我没有在zend框架验证类中找到一个,并且看到了几种实现.

I've been looking for a simple regex for URLs, does anybody have one handy that works well? I didn't find one with the zend framework validation classes and have seen several implementations.

推荐答案

我在一些项目中使用了此方法,我不认为我遇到了问题,但我确定它并不详尽:

I used this on a few projects, I don't believe I've run into issues, but I'm sure it's not exhaustive:

$text = preg_replace(
  '#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i',
  "'<a href=\"$1\" target=\"_blank\">$3</a>$4'",
  $text
);

最后,大多数随机垃圾是用来处理句子中的http://domain.com.之类的情况(以避免匹配尾随时间段).我敢肯定它可以清除,但因为它起作用了.我或多或少只是将其从一个项目复制到另一个项目.

Most of the random junk at the end is to deal with situations like http://domain.com. in a sentence (to avoid matching the trailing period). I'm sure it could be cleaned up but since it worked. I've more or less just copied it over from project to project.

这篇关于URL的PHP​​验证/正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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