Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下正则表达式来验证从页面获取图像:
preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches);
如果匹配/不匹配,我想设置一些条件。我能怎么做?
现在我使用以下方法得到结果:
echo $matches[0][0];
但如果不匹配意味着页面上没有图像:
Notice: Undefined offset: 0
我不知道如何验证 preg_match_all 的结果
只需检查函数的返回值preg_match_all。
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 }