Reputation: 2385
I am pretty sure I know what the issue is, but I cannot resolve it for the life of me and I just don't know what to do at this point.
All I am trying to do is qualify the input of a first and last name field. This code IS working on my local server running php5:
<?php
$regex = "John's Jerod";
if (!preg_match("/^[A-Za-z\'\,\.\s]+$/", $regex)) {
echo "ERROR!";
} else {
echo "NO ERROR!";
}
?>
As expected, this returns NO ERROR!, but when I run this on my live server, I only get ERROR with the same data.
I've determined it's the comma throwing it off! And I am pretty sure, because the php version I am running does an auto escape like \' in the name John\'s, so I ran striptags on all output and still the same error.
DOes anyone know what I am doing wrong or how I can resolve this?
I've got about 8 hours in the "bug" as of now. Been through 40+ variations of the RegEex with no luck.
I check and triple checked all of my form field names, vars etc to ensure everything matcheds and all else is OK.
SOS
Upvotes: 0
Views: 971
Reputation: 15113
Try checking specifically for 1
. Per the documentation, it returns FALSE
(strict check) on error, 0
(strict check) for no matches, or 1
if there is a match (since it stops at one).
Also, per my own preference, I use the ~ symbol for my regex's. And like David Powers said (only he didn't correct it at all), you don't need most of those backslashes (only for the period and the space).
<?php
$regex = "John's Jerod";
if (preg_match("~^[A-Za-z',\.\s]+$~", $regex) !== 1) {
echo "ERROR!";
} else {
echo "NO ERROR!";
}
?>
Hope this helps!
EDIT: Also, you say you're using strip_tags
? That strips any HTML tags in a string -- not slashes. You need strip_slashes
to strip slashes, not strip_tags
;)
Upvotes: 1
Reputation: 1664
Your regex doesn't need all those backslashes. It should be this:
"/^[A-Za-z',\.\s]+$/"
Also, you refer to using striptags(). You should be using stripslashes() if your server has magic quotes enabled.
[Edited regex]
Upvotes: 0