Weird preg_match problems ! Need some help.

Discussion in 'PHP' started by ActiveFrost, Apr 19, 2009.

  1. #1
    String :
    fmt_map=22%2F2000000%2F9%2F0%2F115%2C34%2F0%
    Code (markup):
    What I need to do is to get the first number. In this case, I need to get "22" ( number can be anything - it can be 5, it can be 22, or even 150 ).

    I've the following preg_match :
    preg_match('/fmt_map\=(.*)%/', $content, $fmt);
    PHP:
    Now, the problem is that it does not return me the right value ! Instead of returning 22, it returns the whole ( most part of it ) string.
    Any solutions ?
     
    ActiveFrost, Apr 19, 2009 IP
  2. vetrivel

    vetrivel Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    May be this is not the right solution.but what i will do is
    $fmt_map=22%2F2000000%2F9%2F0%2F115%2C34%2F0%
    $values=explode("%",$fmt_map);
    echo $values[0];
    Still i am saying this is not the solution what you expected.
    But try this.

     
    vetrivel, Apr 19, 2009 IP
    ActiveFrost likes this.
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    Well, I could do it in 5 other ways as well ! I just want to keep my code as clean as possible and do things in the right way :)
    Anyway, thanks for help !

    Still looking for someone who have an idea on how to fix this.
     
    ActiveFrost, Apr 19, 2009 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    For fmt_map at the beginnin ( else '#^\w=(\d+)#' )
    
    $content='fmt_map=22%2F2000000%2F9%2F0%2F115%2C34%2F0%';
    $arr=array();
    if (preg_match('#^fmt_map=(\d+)#',$content,&$arr)) echo $arr[1];
    PHP:
    Regards
     
    koko5, Apr 19, 2009 IP
  5. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #5
    Thank you, works great :)
     
    ActiveFrost, Apr 19, 2009 IP
  6. AviAtriX

    AviAtriX Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    or you could just use explode for % and then get the first value .. but if that works :)
     
    AviAtriX, Apr 19, 2009 IP
  7. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #7
    Someone already mentioned it ( and I already knew that I can do in that way as well ), but thanks anyway :)
     
    ActiveFrost, Apr 19, 2009 IP