我有以下正则表达式来验证从页面获取图像:
preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches);
如果匹配/不匹配,我想提出一些条件。我该怎么办?
现在我使用以下结果获得结果:
echo $matches[0][0];
但如果不匹配则表示页面上没有图片:
Notice: Undefined offset: 0
我不知道如何验证preg_match_all的结果
答案 0 :(得分:3)
只需检查preg_match_all
函数的返回值。
$ret = preg_match_all('~\bhttps?://\S+(?:png|jpg)\b~i', $html, $matches);
if ($ret) {
// matched
// use $matches array for the result
}
else {
// didn't match
}