Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You've made a few errors in the regex. Nothing fatal, I don't think but nevertheless, just a few miscellaneous things you can do to clean it up. You have put parentheses around http, and they don't need to be there. It looks like you're not capturing it for use later. If you're trying to make the http:// part optional, you'll want to use (?:http:\/\/)? instead. Also, note that you need to escape backslashes in a string. Would this work just as well?</p> <pre><code>$regex = "/^". "(?:http:\\/\\/)?". // Look for http://, but make it optional. "(?:[A-Z0-9][A-Z0-9_-]*(?:\\.[A-Z0-9][A-Z0-9_-]*))". // Server name "(?:\\d+)?". // Optional port number "(?:\\/\\.*)?/i"; // Optional training forward slash and page info </code></pre> <p>There are probably better regexes out there for matching URLs. I'd suggest Googling <a href="http://www.google.com/search?q=regex%20url" rel="nofollow">regex url</a> and having a look at them. Don't reinvent the wheel if you don't have to! Also note that the above doesn't allow for URLs without top-level domains, such as <a href="http://localhost/mypage.html" rel="nofollow">http://localhost/mypage.html</a>. If you put a question mark before the double quotes on the "Server name" line, that should allow non-tld URLs.</p> <p>This is a bit inefficient:</p> <pre><code>if(!filter_var($str, FILTER_VALIDATE_URL) || (!preg_match($regex, $str))) //if invalid URL { return FALSE; } else { return TRUE; } </code></pre> <p>Your expression will yield a true/false value. How about just returning that, negating if needed?</p> <pre><code>return !(!filter_var($str, FILTER_VALIDATE_URL) || !preg_match($regex, $str)); </code></pre> <p>Also, note that these expressions are equivalent:</p> <pre><code>!(!A || !B) A &amp;&amp; B </code></pre> <p>So that could be simplified further to just:</p> <pre><code>return filter_var($str, FILTER_VALIDATE_URL) &amp;&amp; preg_match($regex, $str); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload