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

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

PHPz
Release: 2020-09-04 17:53:45
Original
4541 people have browsed it

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);
Copy after login

几乎可以匹配任何网址:

^((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_]*)?$
Copy after login

实例:

Copy after login

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

Related labels:
php
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template