【问题标题】:Remove <select> from html with PHP使用 PHP 从 html 中删除 <select>
【发布时间】:2016-11-04 04:19:04
【问题描述】:

我需要使用 PHP 从 HTML 中删除整个选择下拉列表及其所有选项。

我认为 preg replace 会这样做,但我不知道如何使用它。

所有的html都在一个变量中。

$html = "

<p> something </p>

<select name='bla' id='bla' class='REMOVE'>
<option></option>
<option></option>
<option></option>
<option></option>
</select>

<p> something </p>

<p> something </p>

";

转换为....

$html = "

<p> something </p>

<p> something </p>

<p> something </p>

";

【问题讨论】:

标签: php preg-replace html-parsing


【解决方案1】:

您可以简单地使用 explode()substr()implode() 的组合和一个简单的循环来实现这一点(如果您不想使用正则表达式 - 那就是......) .下面的片段(您可以Quick-Test Here)演示了如何:

<?php

    $html = "

                <p> something </p>

                <select name='bla' id='bla' class='REMOVE'>
                <option></option>
                <option></option>
                <option></option>
                <option></option>
                </select>

                <p> something </p>

                <p> something </p>

                ";

        $arrParts   = explode("</select>", $html);
        foreach($arrParts as &$part){
            if($selPos = strpos($part, "<select")){
                $part = substr($part, 0, $selPos);
            }
        }
        $html = implode("", $arrParts);
        var_dump($html);

【讨论】:

    猜你喜欢
    • 2015-06-09
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 2023-03-19
    • 1970-01-01
    • 2017-08-11
    相关资源
    最近更新 更多