开发者

preg_match assistance needed

Hi I need assistance changing the preg_match to check for 2-16chars A-z 0-9 -_ and space. Right now its checking URL so i'd need to remove the protocol, add space and 2-16 min/max chr.

public function checkUrl($string)
{
    if(empty($string) || preg_match("#^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i", $string))
    {
        return true;
    }
    else
    {
        if( isset开发者_Go百科($this) )
        {
            $this->addError("Input Error");
        }
        return false;
    }
}


I'm not sure what part you want to change, but...

[Need to match] 2-16 chars A-z 0-9 -_ and space.

[\w- ]{2,16}
  • \w matches a-z, A-Z, 0-9 and _.
  • - will match the literal - in a character class outside of a valid range.
  • ' ' will match a space (ignore quotes, Stack Overflow needed them to display). To match any whitespace character, use \s.
  • {2,16} will quantify the match to be between 2 and 16 times inclusively.
  • You could also change (http|https|ftp) to (?:https?|ftp), which won't capture the group, as you aren't using any back references.


According to regular-expressions.info, there are some things you can do:

  • A-Z, a-z, 0-9, and _ can all be matched with \w
  • Min/max can be done with brackets {min, max}

So to check for "2-16chars A-z 0-9 -_ and space" we'd have to do something like this:

[- \w]{2,16}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜