Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “如何解析HTML”;积木;是否为每个非空标记创建表布局?_Php_Html_Regex_Parsing_Dom - Fatal编程技术网

Php “如何解析HTML”;积木;是否为每个非空标记创建表布局?

Php “如何解析HTML”;积木;是否为每个非空标记创建表布局?,php,html,regex,parsing,dom,Php,Html,Regex,Parsing,Dom,更新:(事情更复杂,包括块,我从一开始就没有解释,但我知道这应该适用于正则表达式或其他东西) 如何将HTML块解析为每个非空标记的表布局? 例如,此HTML: <p class="block1"> <span class="styleclass2"> <span class="styleclass25"> <strong> <u></u>Some

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

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

这里有一些文字
www.example.com
这里有一些文字
这里有些文字
www.example2.com
这里有些文字
主要思想是如何将这些块分组,以便为找到的每个块生成一行…

Hodgee code:)


$html=正则表达式很神奇,请尝试以下方法:

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

请发布您遇到问题的PHP代码。我们不会为您编写它。它工作得很好,但是当文本从标记中移出并位于外部标记中时会出现问题,例如:“此处的某些文本”而不是“此处的某些文本
<?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;
?>