 <p>You could save some trouble by using <code>filter_var</code> instead.</p> <pre><code>if (false !== ($url = filter_var($url, FILTER_VALIDATE_URL))) { echo "$url is a valid url"; } </code></pre> <p>You can optionally add these options as the third parameter (use binary or to combine them):</p> <ul> <li><code>FILTER_FLAG_PATH_REQUIRED</code></li> <li><code>FILTER_FLAG_QUERY_REQUIRED</code></li> </ul>