[英]php preg_match url regex not working
我无法正常运行此功能:
function isValidURL($url){
return preg_match('%http://domain\.com/([A-Za-z0-9.-_]+)/([A-Za-z0-9.-_]+)%', $url);
}
网址:
http://domain.com/anything-12/anything-12/
可以包含数字,字母和符号_-
我认为这与第一个正则表达式有关-这些工作
http://domain.com/anything12/anything12/
http://domain.com/anything12/anything-12/
http://domain.com/anything12/any-thing-12/
http://domain.com/anything_12/any-thing-12/
一如既往地感谢所有帮助,并在此先感谢。
您需要在正则表达式的字符类中转义-
。
您需要锚定正则表达式,以便尝试匹配整个输入字符串而不是其中的一部分。
修改后的正则表达式为:
'%^http://domain\.com/([A-Za-z0-9.\-_]+)/([A-Za-z0-9.\-_]+)/$%'
您可以通过注意[A-Za-z0-9_]
与\\w
相同来缩短正则表达式,并且还有重复的子正则表达式。
'%^http://domain\.com(/[\w.-]+){2}/$%'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.