Jump to content

Matching specific table using preg_match


erezep

Recommended Posts

Hello,

i have a a page with a few html tables,

i'm trying to match a specific tables that has some string in it.

 

sample page:

<HTML>

<HEAD>

</HEAD>

<body>

<table align="center" width="771" height="166" border="0" cellpadding="0" cellspacing="0">

<tr> first</tr>

<tr> second</tr>

</table>

<table>

<tr>matchthis</tr>

</table>

</BODY>

</HTML>

 

 

i'm using this regex: <table.+>.+>matchthis<.+<\/table>

 

and i want to get only the latter table :

<table>

<tr>matchthis</tr>

</table>

 

Thanks in advance.

 

<pre>
<?php
$data = <<<DATA
<HTML>
<HEAD>
</HEAD>
<body>
<table align="center" width="771" height="166" border="0" cellpadding="0" cellspacing="0">
<tr> first</tr>
<tr> second</tr>
</table>
<table>
<tr>matchthis</tr>
</table>
</BODY>
</HTML>
DATA;

preg_match('%<table[^>]*>(??!</table>).)*?matchthis.*?</table>%s', $data, $matches);
print_r(htmlspecialchars($matches[0]));

?>
</pre>

Archived

This topic is now archived and is closed to further replies.

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