我在regex php中遇到了问题。需要正则表达式代码才能识别字符串中的链接我的链接:https://mywebsite.com/cargallery/subcat/
if( preg_match( '/(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))(([^"]*cargallery[^"])*([\w_-]+)(\/|))?/', $requested_url,$mactches)) {
// if url = https://mywebsite.com/cargallery/subcat/
echo "true";
}else {
//if url= https://mywebsite.com/cargallery/subcat/dasdsad
// or https://mywebsite.com/cargallery
// or https://mywebsite.com
echo "false";
}
发布于 2018-07-29 15:30:56
对于正则表达式,可以使用anchors断言字符串的起始^
和结束$
。
如果没有锚点,正则表达式将匹配,例如,https://mywebsite.com
和preg_match将返回1。
^(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))(([^"]*cargallery[^"])*([\w_-]+)(\/|))?$
发布于 2018-07-29 05:51:04
只需使用if查看是否匹配即可。没有必要让它变得更复杂。
if($requested_url == "https://mywebsite.com/cargallery/$subcat/"){
echo "true";
}else{
echo "false";
}
https://stackoverflow.com/questions/51575195
复制相似问题