如何用PHP正则表达式实现几乎全范围的网址匹配?

更新于
2026-09-27 02:01:58
31阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计327个文字,预计阅读时间需要2分钟。

如何用PHP正则表达式实现几乎全范围的网址匹配?

比较简单的正则表达式如下:

php$pattern='/^(https?|ftp|rtsp|mms):\/\/[^\s]+$/';$testStr='http://example.com';$matches=[];

if (preg_match($pattern, $testStr, $matches)) { echo 匹配成功:; print_r($matches);} else { echo 匹配失败;}

该表达式可以匹配以http、https、ftp、rtsp或mms开头的网址。

比较简单的

如何用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\-]*[\.。

阅读全文

本文共计327个文字,预计阅读时间需要2分钟。

如何用PHP正则表达式实现几乎全范围的网址匹配?

比较简单的正则表达式如下:

php$pattern='/^(https?|ftp|rtsp|mms):\/\/[^\s]+$/';$testStr='http://example.com';$matches=[];

if (preg_match($pattern, $testStr, $matches)) { echo 匹配成功:; print_r($matches);} else { echo 匹配失败;}

该表达式可以匹配以http、https、ftp、rtsp或mms开头的网址。

比较简单的

如何用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\-]*[\.。

阅读全文