Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.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
在使用文件_get_contents php[使用php]之后,如何按id删除div中的所有数据?_Php_Html_Domdocument_Removechild - Fatal编程技术网

在使用文件_get_contents php[使用php]之后,如何按id删除div中的所有数据?

在使用文件_get_contents php[使用php]之后,如何按id删除div中的所有数据?,php,html,domdocument,removechild,Php,Html,Domdocument,Removechild,如何在使用file\u get\u contentsphp[使用php]后按id删除div中的所有数据 我想删除divid=“one” 这是在https://www.example.com <div id="main"> <div id="inner"> <div id="one"> <div> HELLO </div>

如何在使用
file\u get\u contents
php[使用php]后按id删除div中的所有数据

我想删除div
id=“one”

这是在
https://www.example.com

<div id="main">
    <div id="inner">
        <div id="one">
            <div>
                HELLO
            </div>
            HELLO
        </div>
        <div id="two">
            TEST
        </div>
    </div>
</div>

你好
你好
试验


完成后,我希望得到这样的数据

<div id="main">
    <div id="inner">
        <div id="two">
            TEST
        </div>
    </div>
</div>

试验
我该怎么办

致以最良好的祝愿,
mongkon tiya

编辑:Readed您想要一个简单的PHP解决方案,sry这是一个JS解决方案

echo '<script type="text/javascript">',
     'var elem = document.getElementById("one"); elem.remove();',
     '</script>'
echo',
'var elem=document.getElementById(“一”);elem.remove();',
''
只需在文件获取脚本内容后通过echo调用php即可。我用纯js编写,如果它不工作,并且使用旧浏览器,请使用:

echo '<script type="text/javascript">',
         'var elem = document.getElementById("one"); elem.parentNode.removeChild(elem);
         '</script>
echo',
'var elem=document.getElementById(“一”);elem.parentNode.removeChild(elem);
'

如果您使用Jquery,您也可以只使用函数.remove()

为了仅使用PHP完成这种DOM操作,您需要使用
输出缓冲
DOMDocument

<?php
    ob_start(); /* tell php to buffer the output */
?>

<!-- typical html page - there MUST be a doctype though!-->
<!--
<!doctype html>
<html>
    <head>
        <title>Output buffer test</title>
    </head>
    <body>
        <div id='main'>
            <div id='inner'>
                <div id='one'>
                    <div>
                        Hello World
                    </div>
                    hello
                </div>
                <div id='two'>
                    Goodbye Cruel World
                </div>
            </div>
        </div>
    </body>
</html>
-->

<?php
    echo file_get_contents('http://www.example.com');
?>


<!-- manipulate the DOM  using PHP only -->
<?php
    libxml_use_internal_errors( true );

    /* read the currently stored buffer and load into DOMDocument */
    $buffer=@ob_get_contents();
    $dom=new DOMDocument;
    $dom->loadHTML( $buffer );
    libxml_clear_errors();

    /* Find the DOM element you wish to remove */
    $div=$dom->getElementById('one');
    $div->parentNode->removeChild( $div );

    /* Save the current DOM and flush the buffer */
    $buffer=$dom->saveHTML();
    @ob_end_clean();

    echo $buffer;

    if( @ob_get_level() > 0 ) {
        for( $i=0; $i < ob_get_level(); $i++ ) @ob_flush();
        @ob_end_flush();
    }
    $dom=null;
?>

loadHTML($buffer);
libxml_clear_errors();
/*查找要删除的DOM元素*/
$div=$dom->getElementById('one');
$div->parentNode->removeChild($div);
/*保存当前DOM并刷新缓冲区*/
$buffer=$dom->saveHTML();
@ob_end_clean();
echo$缓冲区;
如果(@ob_get_level()>0){
对于($i=0;$i

您想使用javascript还是仅使用php来执行此操作?-RamRaider,仅使用php Sir它是否始终是要删除的div?您需要使用输出缓冲,然后在将保存的缓冲区刷新到客户端之前,使用DOMDocument进行操作。您能给我一些编码吗?我必须使用php Sir,然后您必须使用php中的DomeElement类,以前从未有人使用过它。使用loadHTML()而不是file_get_内容,并遵循php文档。它的描述非常好。我可以与
file\u get\u contents
PHP一起使用吗?你能给我一些代码吗^ ^ ^我不知道你想在哪里或如何使用
file\u get\u contents
对我有什么作用……也许你可以看看PHP错误日志,看看你有什么。另外,如果我们知道您正在尝试此操作的实际URL,而不是mickey mouse example.com,那将是一件好事
<?php
    ob_start(); /* tell php to buffer the output */
?>

<!-- typical html page - there MUST be a doctype though!-->
<!--
<!doctype html>
<html>
    <head>
        <title>Output buffer test</title>
    </head>
    <body>
        <div id='main'>
            <div id='inner'>
                <div id='one'>
                    <div>
                        Hello World
                    </div>
                    hello
                </div>
                <div id='two'>
                    Goodbye Cruel World
                </div>
            </div>
        </div>
    </body>
</html>
-->

<?php
    echo file_get_contents('http://www.example.com');
?>


<!-- manipulate the DOM  using PHP only -->
<?php
    libxml_use_internal_errors( true );

    /* read the currently stored buffer and load into DOMDocument */
    $buffer=@ob_get_contents();
    $dom=new DOMDocument;
    $dom->loadHTML( $buffer );
    libxml_clear_errors();

    /* Find the DOM element you wish to remove */
    $div=$dom->getElementById('one');
    $div->parentNode->removeChild( $div );

    /* Save the current DOM and flush the buffer */
    $buffer=$dom->saveHTML();
    @ob_end_clean();

    echo $buffer;

    if( @ob_get_level() > 0 ) {
        for( $i=0; $i < ob_get_level(); $i++ ) @ob_flush();
        @ob_end_flush();
    }
    $dom=null;
?>