更新:(事情更复杂,包括块,我从一开始就没有解释,但我知道这应该适用于regex或其他东西)
如何将HTML块解析为每个非空标记的表布局?例如,这个HTML:
<p class="block1">
<span class="styleclass2">
<span class="styleclass25">
<strong>
<u></u>Some Text Here
</strong>
<br>
</span>
</span>
<span class="styleclass5">
<u>
<a href="http://www.example.com">www.example.com</a>
</u>
</span>
<br>
<span class="styleclass24">Some Text Here</span>
</p>
<p class="block2">
<span class="styleclass2">
<span class="styleclass25">
<strong>
<u></u>Some Text Here2
</strong>
<br>
</span>
</span>
<span class="styleclass5">
<u>
<a href="http://www.example2.com">www.example2.com</a>
</u>
</span>
<br>
<span class="styleclass24">Some Text Here2</span>
</p>
并制作这些:
<table>
<tr>
<td>Some Text Here</td>
<td>www.example.com</td>
<td>Some Text Here</td>
</tr>
<tr>
<td>Some Text Here2</td>
<td>www.example2.com</td>
<td>Some Text Here2</td>
</tr>
</table>
主要思想是如何将这些块分组,为找到的每个块排成一行。。。
正则表达式很神奇,试试这个:
<?php
$string = '<p class="styleclass1">
<span class="styleclass2">
<span class="styleclass25">
<strong>
<u>Some Text Here</u>
</strong>
<br>
</span>
</span>
<span class="styleclass5">
<u>
<a href="http://www.example.com">www.example.com</a>
</u>
</span>
<br>
<span class="styleclass24">Some Text Here</span>
</p>';
$result = preg_match_all("/<'w+.*?>(.*?)<'/'w+>/", $string, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
$output = '<table style="border: 1px solid #000;">';
foreach ($matches[1] as $key => $value) {
$output .= '<tr>';
$output .= '<td>'.$value.'</td>';
$output .= '</tr>';
}
$output .= '</table>';
echo $output;
?>
hodgee代码:)
$html = <<<HERE
<p class="styleclass1">
<span class="styleclass2">
<span class="styleclass25">
<strong>
<u>Some Text Here</u>
</strong>
<br>
</span>
</span>
<span class="styleclass5">
<u>
<a href="http://www.example.com">www.example.com</a>
</u>
</span>
<br>
<span class="styleclass24">Some Text Here</span>
</p>
HERE;
preg_match_all('#>(.+)<#sU', $html, $matches);
if(isset($matches[1]))
{
echo '<table border="1">';
foreach($matches[1] as $val)
{
$val = trim($val);
if(!empty($val))
echo '<tr><td>' . $val . '</td></tr>';
}
echo '</table>';
}