0

When $url_xxx = www.googlecom or http://www.5555testetstcom or http://wwwgooglecom it's will echo OK WHY ?

<?PHP
$url_xxx = mysqli_real_escape_string($db_mysqli,$_POST['url_xxx']);
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url_xxx))
{
    echo "URL incorrect format";
}
else
{
    echo "OK";
}
?>

1 Answers1

0

Your Regex is badly shaped. You can decompose it in three parts;

  1. \b(?:(?:https?|ftp)://|www.) (Starts with http or https or such)
  2. [-a-z0-9+&@#/%?=~_|!:,.;]* (Any character)
  3. [-a-z0-9+&@#/%=~_|] (Any character)

Your expression reads "Anything starting with {1}, followed by {2} that can appear from 0 to many times, followed by {3} (which is anything)"

So, well, as {2} is optional due the * modifier, it finally reads like "Anything starting with http:// and followed by anything matches

First I would recommend using Rubular to check your Regex when you build them, it's a REALLY helpful website http://rubular.com . If you would want to fix the regex you should at least require some dots in the middle.

Second, for this particular case you should check this response by Gordon in Best way to check if a URL is valid

Which snipped from there the response is:

if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
    die('Not a valid URL');
}
Community
  • 1
  • 1
Jimmy
  • 1,115
  • 1
  • 9
  • 21