Can it be done with 1 preg_match ?

Discussion in 'PHP' started by linkstraffic, Jun 30, 2006.

  1. #1
    hI,

    Just wondering if it 's possible to get all text included between for instance the <b> & </b> tags from a full page, using only 1 call to preg_match

    So far I can retrieve the info. if there is only one time the tags but when the page contains 2 times these tags, I can not get the right result using:

    preg_match("/<b>(.*)<\/b>",$html,$matches)
    PHP:
    My guess is to do it with a new function.
     
    linkstraffic, Jun 30, 2006 IP
  2. Jean-Luc

    Jean-Luc Peon

    Messages:
    601
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Jean-Luc, Jun 30, 2006 IP
  3. BrianR2

    BrianR2 Guest

    Messages:
    734
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think preg_match is for just one match isn't it? preg_match_all will give you all the matches in an array.

    I think you're missing a '/' at the end of the regex. It would be better to have:

    preg_match_all("/<b>(.*)<\/b>/siU",$html,$matches)
    PHP:
    to make it ungreedy (U) so (.*) doesn't match beyond the bold text and case-insensitive (i) should there happen to be <B></B>. Also, the s at the end will allow for line breaks.
    Now $matches will be an array with all the matches of bold text in $html.
     
    BrianR2, Jun 30, 2006 IP
  4. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #4
    yes right but so far the preg_match returns the text between the first occurence of <b>
    and the last occurence of </b> so it leads to the same result using preg_match_all

    i m looking at it
     
    linkstraffic, Jun 30, 2006 IP
  5. BrianR2

    BrianR2 Guest

    Messages:
    734
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #5
    oh, i see, well the change i just added to my comment will solve that because the U makes it ungreedy.
     
    BrianR2, Jun 30, 2006 IP
  6. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #6
    Yeah , I think I get something, thanks :)
     
    linkstraffic, Jun 30, 2006 IP