livewire1974 Posted February 9, 2012 Share Posted February 9, 2012 I am trying to extract a table from HTML, here is the HTML code for the start of the table. <table class='price' id='comp' style='clear:both;display:none'> But when I use this PHP code, no matches are found preg_match("/<table class='price' id='comp' style='clear:both;display:none'>.*?<\/[\s]*table>/s", $buffer, $matches); print_r($matches); I guess there is something wrong with the preg_match statement? Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/ Share on other sites More sharing options...
AyKay47 Posted February 9, 2012 Share Posted February 9, 2012 if you want to match that exact format, you can use this. $str = "<table class='price' id='comp' style='clear:both;display:none'>"; $pattern = '~<table class=("|\')price("|\') id=("|\')comp("|\') style=("|\')clear:both;display:none("|\')>~'; preg_match($pattern,$str,$matches); print_r($matches); where $matches[0] will contain the matched string. if you want this regex to match a dynamic beginning <table> element, I will have to revise it. Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316227 Share on other sites More sharing options...
livewire1974 Posted February 9, 2012 Author Share Posted February 9, 2012 Hi, thanks for your reply. I am trying to select all of the table, so everything from the opening tag to the </table> tag. Is this possible? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316231 Share on other sites More sharing options...
AyKay47 Posted February 9, 2012 Share Posted February 9, 2012 oh okay, of course it's possible: $str = "<table class='price' id='comp' style='clear:both;display:none'>this is a test</table>"; $pattern = '~<table[^>]*>([^<]*)</table>~'; preg_match($pattern,$str,$matches); print_r($matches); $matches[0] will contain the entire matched string and $matches[1] will contain the text between <table</table> Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316233 Share on other sites More sharing options...
livewire1974 Posted February 9, 2012 Author Share Posted February 9, 2012 But that will select all tables in the HTML file, and not just the tables with class='price' ? Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316234 Share on other sites More sharing options...
AyKay47 Posted February 9, 2012 Share Posted February 9, 2012 you're not giving me enough to go on here, tell me the exact specifications of what table(s) you want to grab, and if you want to grab more than one table. Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316243 Share on other sites More sharing options...
livewire1974 Posted February 9, 2012 Author Share Posted February 9, 2012 Sorry, mistake. All sorted now Thanks for helping out, spent a lot of time messing with this Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316245 Share on other sites More sharing options...
joe92 Posted February 9, 2012 Share Posted February 9, 2012 But that will select all tables in the HTML file, and not just the tables with class='price' ? No. Preg_match will always stop after the first successful match. Preg_match_all however, is the one that will match them all. Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316251 Share on other sites More sharing options...
AyKay47 Posted February 9, 2012 Share Posted February 9, 2012 Sorry, mistake. All sorted now Thanks for helping out, spent a lot of time messing with this im curious to see what the regex you are using looks like? Quote Link to comment https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/#findComment-1316266 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.