English 中文(简体)
php preg_match, not working for validating URL s
原标题:php preg_match, not working for validating URL s

Ihave a PHP preg_match regex which is :

#^(http://|https://|www.|//)*(([A-Z0-9][A-Z0-9_-]*)(.[A-Z0-9][A-Z0-9_-]*)+)(:(d{1,5}))?([A-Z0-9_-]|.|/|?|#|=|&|%)*$#i

问题是,当我试图将任何卢爱拉最终与.合起来的时候;它回来是真实的,但正如预期的那样,它应当退回不实的,与它可能有什么错呢?

我的理解是:

    function isValidURL($url) {
        if (preg_match("#^(http://|https://|www.|//)*(([A-Z0-9][A-Z0-9_-]*)(.[A-Z0-9][A-Z0-9_-]*)+)(:(d{1,5}))?([A-Z0-9_-]|.|/|?|#|=|&|%)*$#i", $url)) {
            return true;
        }else{
            return false;
        }
    }

查阅:

$URL = $_GET[ url ];
echo var_dump(isValidURL($URL));

现在,如果我打开了/url.php?url=http://www.google.com。 如果我开放网页/url.php?url=http://www.google.com**,它就会退回。 如果我打开了/url.php?url=http://www.google.com&& 这种做法是真实的,但应当作为“......com&”归还;“TLD”不是有效的。

问题回答

页: 1

([A-Z0-9_-]|.|/|?|#|=|&|%)*

混凝土机。 如果存在争 ,你可能想要做的是预先确定的问题标记:

(?([A-Z0-9_-]|.|/|?|#|=|&|%)*)?

然而,在PHP5.2+中,这样做有更好的办法:

var_dump(filter_var( http://example.com , FILTER_VALIDATE_URL));

http://www.php.net/manual/en/filter.filters.validate.php





相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签