0

How can I convert this to preg_match?

$urlregex = "(https?|ftp)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&;%$-]+)*@)*((?:(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|[1-9])\.){3}(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|[1-9]|0)|localhost|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:\d+)*(($|[a-zA-Z0-9.,?'+\\&;%\$#=~_-]+))*";

if (!eregi($urlregex, $_POST['url'])) {
    $error = true;
    $res = array(
            'response' => 'error',
            'message' => 'The URL you have entered is invalid.'
    );
}
Bonfocchi
  • 523
  • 3
  • 10
  • 19
  • I have tried: /^$urlregex/i /^$urlregex&/i %^$urlregex%i @^$urlregex@i It always gives me the Unknown modifier error. – Bonfocchi Jul 03 '12 at 17:27

1 Answers1

2

Better use the build in URL validation.

if (filter_var(urldecode($_POST['url']), FILTER_VALIDATE_URL) == FALSE) {
    $error = true;
    $res   = array(
        'response' => 'error',
        'message'  => 'The URL you have entered is invalid.'
    );
}
powtac
  • 40,542
  • 28
  • 115
  • 170