Reputation: 4349
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($this) )
{
$this->addError("Input Error");
}
return false;
}
}
Upvotes: 0
Views: 298
Reputation: 490233
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.(http|https|ftp)
to (?:https?|ftp)
, which won't capture the group, as you aren't using any back references.Upvotes: 3
Reputation: 42496
According to regular-expressions.info, there are some things you can do:
\w
{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}
Upvotes: 2