我想在域名的robots.txt中搜索 Disallow: /
我写了正则表达式,但它没有用。
if(preg_match("!Disallow:\s*/\s\r\n!i",$string,$disallow_char))
{
print_r($disallow_char);
}
以下是两个测试用例 1)
User-agent: *
Disallow: /
2)
User-agent: *
Disallow: /product/generate_pdf/40
Disallow: /news/
Disallow: /news/bollards
Disallow: /product/generate_pdf/44
Disallow: /
Disallow: /page_management/insert
Disallow: /glossary/ajax_call/update_words
两种情况都应输出true。
答案 0 :(得分:2)
您需要断言新行序列或字符串结尾如下:
echo preg_match('~Disallow:\h*/(?:\R|$)~i', $string)
<强>解释强>:
Disallow: # 'Disallow:'
\h* # horizontal whitespace (0 or more times)
/ # '/'
(?: # group, but do not capture:
\R # '\R' (any Unicode newline sequence)
| # OR
$ # before an optional \n, and the end of the string
) # end of grouping