Jump to content


Photo

Preg match question - urgent help needed


  • Please log in to reply
7 replies to this topic

#1 denhamd2

denhamd2
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 25 October 2006 - 10:39 AM

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

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 10:47 AM

Is the variable always only going to be one table, or could it contain more than one?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 denhamd2

denhamd2
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 25 October 2006 - 10:48 AM

just one table

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 10:52 AM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 denhamd2

denhamd2
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 25 October 2006 - 12:29 PM

just everything within the first 7 <tr> tags

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 02:02 PM

ok, give this a try...

<?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];
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 25 October 2006 - 02:06 PM

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

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 02:09 PM

I thought that he still wanted the <tr> tags in there.  My bad.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users