user123
user123

Reputation: 5407

Validate the regex result

I have follwing regex to validate get the image from page:

preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches);

I want to put some condition if matches/not matches. How can I do?

Right now I get the result using:

echo $matches[0][0];

But if does not match means no image on the page :

Notice: Undefined offset: 0 

I dont know how to validate the result of preg_match_all

Upvotes: 1

Views: 51

Answers (1)

anubhava
anubhava

Reputation: 784968

Just check the return value of preg_match_all function.

$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
}

Upvotes: 3

Related Questions