Page 1 of 1

preg_match pattern.

Posted: Mon Jul 16, 2007 8:02 am
by jontore
Hi. I'm having some problems with a method that checks names (e.g First name, Last name) with preg_match(). The pattern I'm using now is ([[:digit:]]|[~`!@#$%^*()_=+{}|\:;"/?,]|[|]|)+';//#^_+=:;()[]<>{}%#$?!\\/0-9]$/';. I'm trying to look for invalid characters but something is wrong because it always return false.

Method:

Code: Select all

function check_text($field_text)
{ 
$pattern = '([[:digit:]]|[~`!@#$%^*()_=+{}|\:;"/?,]|[|]|)+';
if(preg_match($pattern,$field_text))
  {
  	return true;
  }
 return false;
}
Thanks for any help :)

Posted: Mon Jul 16, 2007 8:13 am
by feyd
Your pattern will fail compilation with preg_*.

Posted: Mon Jul 16, 2007 8:20 am
by jontore
What if I use ereg instead? Is there another way to formulate my pattern ?

Posted: Mon Jul 16, 2007 10:09 am
by vigge89
Your pattern does not have any starting or ending delimeters (required for the PCRE functions).
PHP Manual: CXX. Regular Expression Functions (Perl-Compatible):
The syntax for patterns used in these functions closely resembles Perl. The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.