I'm very new to regular expressions and need a little help with something complicated.
I have a list of URLs that may as well be in an array that would look like this:
$urls = array(
"http://example.com/page.php",
"http://example.com/page.php?key=value",
"http://example.com/image.jpg",
"http://example.com/image.jpg?key=value" ...
I want to loop over the array (which is simple enough with foreach) and for each string return true if the URL points to a file that is an image. I have the following regular expression:
"#\.(jpg|jpeg|gif|png)$# i"
... but it seems to only return true is the string ends in one of the given image extensions. I need to compensate for two factors: 1. if the string has a URL query string on the end of it (ie. ?key=value) and whether the extenion (eg. jpg) is actually part of the query string to a non-image file, for example:
http://example.com/page.php?image=file.jpg
Should return false because the URL is pointing to a PHP file, not a jpg
Thank you for any help!
page.phpdoes not return an image'~^(?!.*\?.*(\.(?:jpg|jpeg|gif|png)\b)).*(?1)(?:$|\?)~'