eregi regex issues - Help!

Discussion in 'PHP' started by LazyD, Mar 9, 2007.

  1. #1
    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)
     
    LazyD, Mar 9, 2007 IP
  2. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #2
    srobona, Mar 9, 2007 IP
  3. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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..
     
    LazyD, Mar 9, 2007 IP
  4. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #4
    can u please explain more?
     
    srobona, Mar 9, 2007 IP
  5. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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...
     
    LazyD, Mar 9, 2007 IP
  6. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    srobona, the solution you sent me via PM did not work, thanks for trying...
     
    LazyD, Mar 9, 2007 IP
  7. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    Robert Plank, Mar 10, 2007 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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:
     
    exam, Mar 10, 2007 IP
  9. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    LazyD, Mar 10, 2007 IP
  10. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #10
    
    
    <?
    
    $s = 'New+Hampshire1';
    if (preg_match('/^[a-z\+]*$/i',$s))
     echo 'OK';
    else
     echo "Invalid";
    ?>
    
    
    PHP:
     
    wmtips, Mar 11, 2007 IP