preg_match pattern.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jontore
Forum Newbie
Posts: 2
Joined: Mon Jul 16, 2007 7:56 am

preg_match pattern.

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your pattern will fail compilation with preg_*.
jontore
Forum Newbie
Posts: 2
Joined: Mon Jul 16, 2007 7:56 am

Post by jontore »

What if I use ereg instead? Is there another way to formulate my pattern ?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
Post Reply