denhamd2 Posted October 25, 2006 Share Posted October 25, 2006 Hi,I have some html stored in a variable $myhtml with a <table> with a few rows. There are currently a lot of rows but I basically just want to echo the first 7 rows, i.e. only take up to the 7th occurance of </tr> ... any ideas on how to do this? Many thanks in advance... Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/ Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 Is the variable always only going to be one table, or could it contain more than one?RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/#findComment-114115 Share on other sites More sharing options...
denhamd2 Posted October 25, 2006 Author Share Posted October 25, 2006 just one table Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/#findComment-114117 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 ok, and do you want complete code returned or just partial code? I'll explain the question. Do you want the <table> tag along with the first 7 <tr> tags, or just the 7 <tr> tags, or the <table>, the first 7 <tr> tags and then everything after the last <tr> tag?Long winded, but hopefully makes sense...RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/#findComment-114119 Share on other sites More sharing options...
denhamd2 Posted October 25, 2006 Author Share Posted October 25, 2006 just everything within the first 7 <tr> tags Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/#findComment-114156 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 ok, give this a try...[code]<?php// Maximum number of rows to display$max = 7;preg_match_all('|<tr>.*?</tr>|ms', $myhtml, $matches);for ($i = 0; $i < $max; $i++){ echo $matches[0][$i];}?>[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/#findComment-114205 Share on other sites More sharing options...
effigy Posted October 25, 2006 Share Posted October 25, 2006 [code]<pre><?php $html = <<<HTML <table> <tr><td>1</td></tr> <tr><td>2</td></tr> <tr><td>3</td></tr> <tr><td>4</td></tr> <tr><td>5</td></tr> <tr><td>6</td></tr> <tr><td class="something">7</td></tr> <tr><td>8</td></tr> <tr><td>9</td></tr> <tr><td>10</td></tr> </table>HTML; preg_match_all('%<tr[^>]*>(.*?)</tr>%', $html, $matches); $first_7 = array_slice($matches[1], 0, 7); print_r($first_7); ?></pre>[/code] Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/#findComment-114206 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 I thought that he still wanted the <tr> tags in there. My bad.RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/25038-preg-match-question-urgent-help-needed/#findComment-114208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.