Home >Backend Development >PHP Tutorial >PHP正则表达式如何匹配任意类型网址

PHP正则表达式如何匹配任意类型网址

PHPz
PHPzOriginal
2016-06-06 20:45:024495browse

PHP正则表达式匹配任意类型网址的方法:首先打开相应的代码文件;然后修改匹配网址的正则为“^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|...)”;最后保存即可。

PHP正则表达式如何匹配任意类型网址

PHP正则表达式如何匹配任意类型网址?

php匹配网址的正则 几乎可以匹配任何网址,需要的朋友可以参考下

 

比较简单的

$str = '';
$isMatched = preg_match('/^^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+$/', $str, $matches);
var_dump($isMatched, $matches);

几乎可以匹配任何网址:

^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-z][a-z0-9_]*)?$

实例:

更多相关技术文章,请访问PHP中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn