Help creating patern for preg_match

Discussion in 'PHP' started by PinoyIto, Aug 5, 2011.

  1. #1
    Hello Guys,

    Will you please help me to figure out how to create a pattern for my preg_match.

    Here is a example text

    <td align=center><font size=3>First text to extract</font></td>
    <td align=center><font size=3>second text to text to extract</font></td>
    <td align=center><font size=3>second text to text to extract</font></td>

    What I want to get are those text between <td align=center><font size=3> and </font></td>

    I'll appreciate the helps...

    BTW, I tried the following but no success
    preg_match('/<td align=center><font size=3>(.*?)</font></td>/i', $longtext, $extracted);
    
    echo $extracted[0];
    Code (markup):
     
    PinoyIto, Aug 5, 2011 IP
  2. echipvina

    echipvina Active Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    preg_match('{\<td\salign=center\>\<font\ssize=3\>(.*?)\</font\>\</td\>}', $longtext, $extracted);
    var_dump($extracted);
    
    PHP:
    Note:Metacharacters (must be escaped)
    http://www.ipaste.org/mg
     
    echipvina, Aug 6, 2011 IP
  3. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #3
    thanks for the help, but didn't solve the problem
     
    PinoyIto, Aug 6, 2011 IP
  4. tulvit

    tulvit Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    preg_match() will stop searching after the first match, if you want to search for all matches, you should use preg_match_all:

    $longtext = 
     '<td align=center><font size=3>First text to extract</font></td>
      <td align=center><font size=3>second text to text to extract</font></td>
      <td align=center><font size=3>third text to text to extract</font></td>';
    
    preg_match_all('#<td align=center><font size=3>(.*?)</font></td>#i', $longtext, $matches, PREG_SET_ORDER);
    
    /*
    print_r($matches); should return something like this:
    Array
    (
        [0] => Array
            (
                [0] => <td align=center><font size=3>First text to extract</font></td>
                [1] => First text to extract
            )
    
        [1] => Array
            (
                [0] => <td align=center><font size=3>second text to text to extract</font></td>
                [1] => second text to text to extract
            )
    
        [2] => Array
            (
                [0] => <td align=center><font size=3>third text to text to extract</font></td>
                [1] => third text to text to extract
            )
    
    )
    Where $matches[0][1], $matches[1][1] and $matches[2][1] - text between the tags.
    */
    PHP:
     
    tulvit, Aug 6, 2011 IP