Jump to content

Recommended Posts

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

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...

Regards
Huggie
[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]
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.