PCRE模式与网站URL匹配
问题描述:
我正在尝试配置网站插件,并且需要为我的网站上的某些网页设置PCRE网址模式。 我需要排除的是以下模式页:PCRE模式与网站URL匹配
http://www.autonews.com/automarket_news/news/1800699/
http://www.kolesa.com/news/sometexthere/
会有人知道如何做到这一点使用PCRE表达式..?
答
编写正确的regex
可以匹配任何有效的URL并不是一件容易的工作。幸运的是,没有必要编写这样的regex
,PHP
提供了更好的工具。
使用功能parse_url()
分裂URL
到部件,然后使用简单的比较,查找(或者甚至是简单的regex
ES如果你真的需要他们)来分析URL件,并决定下一步该怎么做。
示例代码:
$url = 'http://www.autonews.com/automarket_news/news/1800699/etc';
$pieces = parse_url($url);
if ($pieces['host'] == 'www.autonews.com' &&
strpos($pieces['path'], '/automarket_news/news/1800699/') === 0) {
// matches, do something
} else {
// doesn't match, do something else
}