Preg_match help..

Discussion in 'PHP' started by terryuk, May 14, 2007.

  1. #1
    I'm trying to get something to work but its just not happening :eek:

    Say i have "10 of about 14 from" in a string.

    I'm trying to use preg_match to get the value inbetween 'about' and 'from' so if it were to work it would return 14?

    Any ideas please :(
     
    terryuk, May 14, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    $string = '10 of about 14 from';
    if( !preg_match('~about ([0-9]+) from~si', $string, $match ) )
      die("Invalid string passed");
    
    echo $match[1];
    ?>
    
    PHP:
     
    krakjoe, May 14, 2007 IP
    terryuk likes this.
  3. terryuk

    terryuk Notable Member

    Messages:
    3,962
    Likes Received:
    319
    Best Answers:
    0
    Trophy Points:
    255
    #3
    Could anyone please tell me if it would be different if i had '10 of about 1,400 from' rather than '10 of about 14 from' and still using the same expression?
     
    terryuk, May 14, 2007 IP
  4. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #4
    yeah above code only grab numeric character!

    if you want it to include the comma(s) in grabbing:
    
    <?php
    $string = '10 of about 1,400 from';
    if( !preg_match('~about ([0-9,]+) from~si', $string, $match ) )
      die("Invalid string passed");
    
    echo $match[1];
    ?>
    Code (markup):
     
    grandpa, May 14, 2007 IP
    terryuk likes this.