Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with preg_match

Status
Not open for further replies.

Kerflumper

IS-IT--Management
Mar 1, 2010
28
US
I'm trying to setup an email form on a Godaddy server but it doesn't seem to like my preg_match functions (the form works otherwise, btw). I have the identical script on other servers with no issue. Here are the relevant snips:

Code:
$firstname=$_POST['firstname'];
$safefirstnamepattern = "/^[a-z \-.'()]+$/i";
if (preg_match($safefirstnamepattern,$firstname) === 0) {
	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=formerror.php">';
		exit();
	 }

The message I get is Warning: preg_match() [function.preg-match]: Unknown modifier ']'

What's wrong here?
 
I posted the wrong pattern that was causing the error. The correct pattern is "/^[a-z0-9 \-,.:?'()/]+$/i". Do I have a / in the wrong place?

 
I think you should be escaping the special characters, including the slash in the middle of your expression.

-----------------------------------------
I cannot be bought. Find leasing information at
 
My understanding was that the \ was escaping the -. I changed the pattern to "/^[a-z0-9 \-,.:?'()]+$/i" and the error went away. Hopefully that was the problem and it will work as it should.
 
you do not need to escape characters in a character class.

the problem in your first post was the it was enclosed in double quotes rather than single quotes. so php was trying to expand the dollar sign as a variable.
 
you are quite right that the dash DID need escaping as per my referred post. the php manual says
php manual said:
All non-alphanumeric characters other than \, -, ^ (at the start) and the terminating ] are non-special in character classes, but it does no harm if they are escaped. The pattern terminator is always special and must be escaped when used within an expression.

My feeling is that the dollar sign was causing problems but the error message you were getting does not actually point to that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top