Regular Expression Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mich dobelman

    Regular Expression Problem

    I am trying to extract hello world out of the following html string if
    the "message" string is found in the same row.

    I am not sure why it's not working.

    <?php
    $string = '<html>
    <body>
    <table>
    <tr>
    <td class="bg-grey-m">
    <table cellpadding="12 ">
    <tr><td>message </td><td><i>hello world</i></td></tr>
    <tr><td>tel</td><td><i>111-111-555</i></td></tr>
    </table>
    </td>
    </tr>
    </table>
    </body>';

    preg_match_all( "/<td
    [^>]*class=\"bg-grey-m\">[^<]+<table[^>]*>.*message.*<i >(.*)<\/i>.*<\/table>.*/iU",
    $string, $matches);

    foreach($matche s[1] as $link)
    {
    echo "<li>$link</li>\n";
    }


    ?>


  • BinnyVA

    #2
    Re: Regular Expression Problem

    Hi,
    I am trying to extract hello world out of the following html string if
    the "message" string is found in the same row.
    preg_match_all( "/<td
    [^>]*class=\"bg-grey-m\">[^<]+<table[^>]*>.*message.*<i >(.*)<\/i>.*<\/table>.*/iU",
    $string, $matches);
    Try...

    preg_match_all( '/<tr><td>message <\/td><td><i>(.+?) <\/i><\/td><\/tr>/',$string,
    $matches);

    Hope this is what you are looking for.

    --
    Binny V A
    http://www.bin-co.com/ - All about Scripting Languages in Web
    Development

    Comment

    • mich dobelman

      #3
      Re: Regular Expression Problem

      Thank you for your reply.

      I wanted to carry bg-grey-m class just in case similar table happened to be
      found.
      However, after all, putting /s switch solved all the problem.
      I haven't figured out what /s is for.

      preg_match_all( "/<td
      [^>]*class=\"bg-grey-m\">[^<]+<table([^>]*)>.*<td[^>]*>message<\/td><td[^>]*><i>(.*)<\/i><\/td>.*<\/table>/siU",
      $string, $matches);

      foreach($matche s[2] as $link)
      {
      echo "<li>$link</li>\n";
      }



      Comment

      • Rik

        #4
        Re: Regular Expression Problem

        mich dobelman wrote:
        Thank you for your reply.
        >
        I wanted to carry bg-grey-m class just in case similar table happened to be
        found.
        However, after all, putting /s switch solved all the problem.
        I haven't figured out what /s is for.
        With /s the dot (.) matches newline characters. I assume there were
        line-breaks in the HTML :-).

        Grtz,
        --
        Rik Wasmus

        Comment

        Working...