Hey all, quick eregi regex question.... I am trying to sanitize/validate variables containing state and county names...As you know, some are 2 words, I am trying to get them to pass through ereqi but im having issues with 2 worded state/county names. They are formatted like: California (Simple 1 word, works fine) New+Hampshire (I cant get the + to work in my regex)
i think u can use ereg_replace first to remove the + sign, then use eregi function. better see here: http://www.php.net/manual/en/function.eregi.php
Well, the variables I am getting are $_GET variables, I want to check them before I pass them to MySQL.... Im not sure replacing it is what I had in mind..
So, for instance a URL could look like - http://mysite.com/state.php?State=New+Hampshire.html The New+Hampshire is then put into a variable from $_GET I want to use regex through eregi or even preg_match to check New+Hampshire for invalid characters, I only want a-zA-Z and + to be valid, anything else I want to fail validation, the problem is no matter how I try I can never get the + to validate...
Your problem with the plus sign is probably you are not escaping the plus sign... use \+ in your regex not + But the proper way to do it would be to run the text through urldecode() first... and then the plus sign would become a regular space character.
The preg_* functions are usually faster than the ereg_* functions. if (preg_match ('%[^a-z\.\+]%i', $_GET['State'])) { // Something other than a letter a period (.) or a plus sign (+) was found in the State variable. } else { // We're good to go. } PHP:
I got it resolved on Namepros, turns out, similar to what Robert said, when the $_GET variabel containing the state name was run through preg_match it was automatically replacing the "+" with " " causing it to fail validation... Thanks all for your help
<? $s = 'New+Hampshire1'; if (preg_match('/^[a-z\+]*$/i',$s)) echo 'OK'; else echo "Invalid"; ?> PHP: