Jump to content

preg_match(_all)


Aureole

Recommended Posts

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

 

Say the source of the web page I am using file_get_contents on has

 

               <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 did a preg_match for <td class="values">(.*?)</td> i.e.

<?php preg_match('/<td class="values">(.*?)<\/td>/', $file, $result); ?>

and it worked fine but

<?php preg_match('/<td class="col2">(.*?)<\/td>/', $file, $result); ?>

didn'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... thanks.

Link to comment
Share on other sites

  • 2 weeks later...

maybe putting them all into one pattern wud help u better?

preg_match_all('@(<td\s+(?:class)?\s*=\s*"(.*)"\s*>(.*?)</td\s*>)@i',$file,$result);

 

which resulted in a print_r like so:

Array
(
    [0] => Array
        (
            [0] => <td class="values">778</td>
            [1] => <td class="col1 left">Earned</td>
            [2] => <td class="col2">49 of 49</td>
        )

    [1] => Array
        (
            [0] => <td class="values">778</td>
            [1] => <td class="col1 left">Earned</td>
            [2] => <td class="col2">49 of 49</td>
        )

    [2] => Array
        (
            [0] => values
            [1] => col1 left
            [2] => col2
        )

    [3] => Array
        (
            [0] => 778
            [1] => Earned
            [2] => 49 of 49
        )

)

 

which now ya can move array 2 & 3 into an array pair

$values=array_combine($resullt[2],$result[3]);

 

which will give you access to your vars as

$values['values']

$values['col1 left']

$values['col2']

 

 

Link to comment
Share on other sites

I just did:

 

<?php
str_replace( "\n", "", $file );
str_replace( "\r", "", $file );
str_replace( " ", "", $file );
?>

 

...and that got rid of most of the problems, the rest of the problems were because the file I am opening is amazingly large and preg_match can only read so many bytes as far as I could find out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.