-3

Is there a simpler way of validating an URL?

$website = test_input($_POST["website"]);

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
    $websiteErr = "Invalid URL"; 
}
Jon
  • 12,684
  • 4
  • 31
  • 44
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69

3 Answers3

3

User the FILTER_VALIDATE_URL with the filter_var method:

filter_var docs: http://nl1.php.net/filter_var

filters you can use, including URL http://nl1.php.net/manual/en/filter.filters.validate.php

timing
  • 6,340
  • 1
  • 17
  • 16
3
<?php
$url = "http://www.myweb.com";

if(!filter_var($url, FILTER_VALIDATE_URL))
{
echo "Not valid URL";
}
else
{
echo "Valid URL";
}
?> 
CS GO
  • 914
  • 6
  • 20
1

You may try this

/^HTTP|HTTPS|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62