Php 字符串的正则表达式(图像url检查)
我需要一个字符串的正则表达式是一个图像url。 我需要三种类型的正则表达式Php 字符串的正则表达式(图像url检查),php,regex,preg-match,Php,Regex,Preg Match,我需要一个字符串的正则表达式是一个图像url。 我需要三种类型的正则表达式 以斜杠开头(示例:/p/230x230/9/Apple_iPad_2_16GB@@9ap4d206.png) 以双斜杠开始(例如://image) 从http开始(例如:'http://....") 您可以使用以下选项: $pattern = '~(?>https?+:/|/)?(?>/[^/\s]++)+~'; 说明: (?> # open an atomic group *
$pattern = '~(?>https?+:/|/)?(?>/[^/\s]++)+~';
说明:
(?> # open an atomic group *
https?+ # http or https
:/ #
| # OR
/
)? # close the atomic group and make it optional
(?> # open an atomic group
/
[^/\s]++ # all characters except / or spaces one or more times (possessive *)
)+ # close the atomic group, one or more times
(*有关和的更多信息。)
注意:
由于模式描述的url充满斜杠,因此我使用~
作为分隔符,而不是传统的/
。因此,斜杠不需要在模式中转义
您可以向该模式添加锚点,以确保从头到尾与字符串完全匹配:
$pattern = '~^(?>https?+:/|/)?(?>/[^/\s]++)+$~';
如果您想通过检查文件扩展名是否为已知的图像扩展名来检查url是否指向图像,但实际上并不起作用。不,我不是在检查扩展名,我只需要检查字符串的起始字符
preg\u match('/^(\/)(http)/','http')
?