Regular Expression

Associate
Joined
26 Jun 2003
Posts
1,140
Location
North West
Im trying to make sure a user can only enter a string which has characters [a-zA-Z_0-9] (This eqivalent to '\w'?), but it seems to not work.

If the string only contains these characters do this, otherwise do that.

PHP:
if(!preg_match('\w', $string))
  postError;
else
  continue;

Can anyone see why is isnt working?
 
PHP:
if (!preg_match('[a-zA-Z_0-9]+', $string))
{
// bla bla
}

Returns the following error:

Warning: preg_match() [function.preg-match]: Unknown modifier '+' in mypage.php on line 19
 
No errors this time but this string was accepted:

23424\"\"%\"&\"\"

Any Ideas?

Im running the string through strip_tags first:

$string = strip_tags($_POST['variable']);

!preg_match(.....
 
Back
Top Bottom