V@no
10-09-2009, 02:38 PM
Hello!
I've been struggling for sometime now trying figure out how to capture data between ALL <td></td> tags and it must be sorted by <tr></tr> (meaning I need to know in which <tr> the <td> is located)
As an working example I'm using this code:
<?php
function highlight($array)
{
foreach($array as $key => $val)
if (is_array($val))
$array[$key] = highlight($val);
else
$array[$key] = '<span style="color:red;font-weight:normal;">'.(preg_match("#^[\n\r]#", $val) ? "" : "\n").htmlspecialchars($val).'</span>';
return $array;
}
$text = '
<table>
<tr>
<td>tb1-tr1-td1</td>
<td>tb1-tr1-td2</td>
<td>
<span>
tb1-tr1-td3
<span>
</td>
<td>
tb1-tr1-td4
</td>
</tr>
<tr>
<td>tb1-tr2-td1</td>
<td><span>tb1-tr2-td2</span></td>
<td>
tb1-tr2-td3
</td>
</tr>
</table>
<table>
<tr>
<td>tb2-tr1-td1</td>
<td><span>tb2-tr1-td2</span></td>
<td>
tb2-tr1-td3
</td>
</tr>
</table>
';
preg_match_all('#<table>(\s*<tr>(\s*<td>(.*)</td>\s*)+</tr>\s*)+</table>#sU', $text, $result);
echo "<pre><b>";
print_r(highlight($result));
echo "</b></pre>";
?>
It returns data from the last <tr> and last <td></td> only of each <table>...what am I doing wrong?
Thank you.
I've been struggling for sometime now trying figure out how to capture data between ALL <td></td> tags and it must be sorted by <tr></tr> (meaning I need to know in which <tr> the <td> is located)
As an working example I'm using this code:
<?php
function highlight($array)
{
foreach($array as $key => $val)
if (is_array($val))
$array[$key] = highlight($val);
else
$array[$key] = '<span style="color:red;font-weight:normal;">'.(preg_match("#^[\n\r]#", $val) ? "" : "\n").htmlspecialchars($val).'</span>';
return $array;
}
$text = '
<table>
<tr>
<td>tb1-tr1-td1</td>
<td>tb1-tr1-td2</td>
<td>
<span>
tb1-tr1-td3
<span>
</td>
<td>
tb1-tr1-td4
</td>
</tr>
<tr>
<td>tb1-tr2-td1</td>
<td><span>tb1-tr2-td2</span></td>
<td>
tb1-tr2-td3
</td>
</tr>
</table>
<table>
<tr>
<td>tb2-tr1-td1</td>
<td><span>tb2-tr1-td2</span></td>
<td>
tb2-tr1-td3
</td>
</tr>
</table>
';
preg_match_all('#<table>(\s*<tr>(\s*<td>(.*)</td>\s*)+</tr>\s*)+</table>#sU', $text, $result);
echo "<pre><b>";
print_r(highlight($result));
echo "</b></pre>";
?>
It returns data from the last <tr> and last <td></td> only of each <table>...what am I doing wrong?
Thank you.