0

如何将其转换为 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.'
    );
}
4

1 回答 1

2

更好地使用内置 URL 验证

if (filter_var(urldecode($_POST['url']), FILTER_VALIDATE_URL) == FALSE) {
    $error = true;
    $res   = array(
        'response' => 'error',
        'message'  => 'The URL you have entered is invalid.'
    );
}
于 2012-07-03T17:26:04.083 回答