how can i filter string using regular expression

Discussion in 'PHP' started by tvshock, Dec 10, 2009.

  1. #1
    hello

    can i filter the string using regular expression. example like this

    $patern = "/^[a-zA-Z0-9\ ]+$/";

    if(preg_match($patern,$some_random_string))
    {
    // some statement
    }

    if i do like that. it only check the string true or false right? .

    my question is how can i remove the string except letter and number only.

    if i do like this

    $some_random_string = str_replace('#;','',$some_random_string );
    $some_random_string = str_replace("'',"",$some_random_string);

    it not very nice because the script too long. that is only 2 string i remove. what about if i want to remove many string. the script will be so long. and hard to edit back later.

    if i can remove string using regular expression it will be great. i try like this

    $patern = "/^[a-zA-Z0-9\ ]+$/";

    $res = preg_match($patern,$some_random_string);

    but the output show 0 or 1 . means true or false. it not filter that string.


    sorry if my english bad.
     
    tvshock, Dec 10, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    use preg_replace
     
    javaongsan, Dec 10, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $new_string = preg_replace('/[^a-z0-9]+/i', ''. $some_random_string);
    PHP:
     
    JAY6390, Dec 11, 2009 IP
  4. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #4
    If regex confuses the hell out of you (like it does me), you can always do this:

    $dirty = array( "#", ":", "?" );
    $string = str_replace($dirty, "", $string);
    PHP:
    That will remove each occurrence of each item in $dirty.
     
    Wogan, Dec 11, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    True, but surely the other way is easier
     
    JAY6390, Dec 11, 2009 IP
  6. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #6
    If you know regex, yeah.
     
    Wogan, Dec 11, 2009 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well the regex is fairly easy to understand.

    If a character isn't a-z or 0-9 then replace it with '' (or a null string)

    If you were to do it you way then it would be easier to have
    $dirty = str_split('#:?');
    PHP:
     
    JAY6390, Dec 11, 2009 IP
  8. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #8
    Ahh, my bad - misread the question. I thought there were just specific characters that needed removing. Yes, if it's everything-but, regex is definitely the way to go.
     
    Wogan, Dec 11, 2009 IP