0

i have a function which checks whether a URL contains any special characters and i want to add '%' to the allowed as i'm using %PREFIX% as a container. I'm relatively new to Regex and was wondering if someone could help me to add the % to the allowed list. The function is the below:

function specialChars($url)
{       
    if (preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url) != true)
    {
        $this->errors[] = "Please remove any special characters";           
    }       
}

Thanks in advance. Regards,

4

1 回答 1

0

正如http://www.regular-expressions.info/reference.html所解释的,[](及其内容)定义了一个字符类。

由于 % 不是字符类(或根本不是)内的元字符,因此您可以将其放置在您希望允许它进入的字符类中,例如[a-z0-9-%]

于 2013-05-06T07:05:30.587 回答