PHP regular expression to detect if url is to an image file
<p>I'm very new to regular expressions and need a little help with something complicated.</p>

<p>I have a list of URLs that may as well be in an array that would look like this:</p>

<pre><code>$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" ...
</code></pre>

<p>I want to loop over the array (which is simple enough with <code>foreach</code>) and for each string return true if the URL points to a file that is an image. I have the following regular expression:</p>

<pre><code>"#\.(jpg|jpeg|gif|png)$# i"
</code></pre>

<p>... 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. <code>?key=value</code>) and whether the extenion (eg. <code>jpg</code>) is actually part of the query string to a non-image file, for example:</p>

<pre><code>http://example.com/page.php?image=file.jpg
</code></pre>

<p>Should return false because the URL is pointing to a PHP file, not a jpg</p>

<p>Thank you for any help!</p>