Aureole Posted January 18, 2008 Share Posted January 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
Aureole Posted January 18, 2008 Author Share Posted January 18, 2008 Nobody have any ideas? This is really frustrating me. Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 18, 2008 Share Posted January 18, 2008 Both your patterns work fine within the haystack you provided. If it doesn't work the problem is coming from somewhere else, verify your content from file_get_contents to be what you think it is when you have these "problems" Quote Link to comment Share on other sites More sharing options...
laffin Posted January 26, 2008 Share Posted January 26, 2008 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'] Quote Link to comment Share on other sites More sharing options...
Aureole Posted January 26, 2008 Author Share Posted January 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.