preg_match

Discussion in 'Programming' started by darkhorn, Nov 7, 2008.

  1. #1
    ...<img src="/img_box/wind-13x13/w045.gif" alt="NE" width="13" height="13" /> 22 kmh
                          </p>...
    
    PHP:
    I want to get the speed (here it is 22). My idea is to get second and third charecters before kmh. But i don not have an idea how to do it? Can you help me?
    note: "/img_box/wind-13x13/w045.gif", "NE" and "22" are not static.
     
    darkhorn, Nov 7, 2008 IP
  2. darkhorn

    darkhorn Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    solved :)

    echo "<br>";
    
    <? $data = "The party will start at 22 kmh";
    preg_match_all('/(\d+)\s*(kmh|kml)/', $data, $match, PREG_PATTERN_ORDER);
    echo "Full: <br>";
    print_r($match[0]);
    echo "<p>Raw: <br>";
    print_r($match[1]);
    echo "<p>Tags: <br>";
    print_r($match[2]);
    ?> 
    PHP:
    output:

    Full: <br>Array
    (
        [0] => 22 kmh
    )
    <p>Raw: <br>Array
    (
        [0] => 22
    )
    <p>Tags: <br>Array
    (
        [0] => kmh
    )
    
    HTML:
     
    darkhorn, Nov 7, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    I would add the <img> marker before the \d so the pattern is more specific.

    /<img[^>]+>\s*(\d+)\s*(kmh|kml)/is
    Code (markup):
     
    joebert, Nov 7, 2008 IP
  4. Pocahontas

    Pocahontas Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks, it helped me solving a similar problematic code
     
    Pocahontas, Nov 9, 2008 IP
  5. darkhorn

    darkhorn Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    darkhorn, Nov 9, 2008 IP