0

更新:(事情更复杂,包括blocks,我从一开始就没有解释,但我知道这应该适用于正则表达式或其他东西)

如何将 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>

主要思想是如何对这些块进行分组,以便为​​找到的每个块排成一行……

4

2 回答 2

1

正则表达式很神奇,试试这个:

<?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;
?>
于 2013-02-21T15:39:10.853 回答
0

大杂烩代码:)

$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>';
}
于 2013-02-21T15:37:46.810 回答