Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    42
    Thanks
    4
    Thanked 4 Times in 4 Posts

    preg_match(_all)

    I'm using preg_match and preg_match_all on the contents of a file I retrieved using file_get_contents. Sometimes it works fine and sometimes it just refuses to work.

    Say the source of the web page has
    Code:
                    <tr>
                        <td>Played</td>
                        <td class="values">778</td>
                     </tr>
                    <tr>
                        <td class="col1 left">Earned</td>
                        <td class="col2">49 of 49</td>
                    </tr>
    I do a preg_match

    Code:
    <?php preg_match('/<td class="values">(.*?)<\/td>/', $file, $result); ?>
    and it works fine but then I do another

    Code:
    <?php preg_match('/<td class="col2">(.*?)<\/td>/', $file, $result); ?>
    and it doesn't work.

    Anyone have any ideas? The problem happens with both preg_match and preg_match_all, sometimes when I search for something it works fine and other times it doesn't... it's really annoying. Thanks a lot.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by AoR Zeta View Post
    Anyone have any ideas? The problem happens with both preg_match and preg_match_all, sometimes when I search for something it works fine and other times it doesn't... it's really annoying.
    PHP Code:
    <?php preg_match('/<td class="col2">([^<]*)<\/td>/'$file$result); ?>
    but this select only the elements wiyh class="col2'

    to select td for any class

    PHP Code:
    <?php preg_match('/<td class="[^"]+">([^<]*)<\/td>/'$file$result); ?>
    check also the semantics for arg 2 and 3 of the preg_match against what you want/have in $file and $result.

    best regards
    Last edited by oesxyl; 01-18-2008 at 08:23 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •