我&#39;试图删除HTML代码类。但是doesen没有工作。我的问题是,我不知道如何删除特定的<div>
。我只有错误:
传递给DOMXPath :: __ construct()的参数1必须是DOMDocument的实例,字符串在
中给出
它的HTML结构:
<section id="tresc" >
<div class="todelete1">
<strong >text to delete</strong>
</div>
<b>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</b>
<div id="todelete_2" class="todelete2">
<script type="text/javascript">
GR("blabla","center");
</script>
</div>
<br>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.<br>
<br><i>
<a href="http://www.lipsum.com/" class="lipsum">LIPSUM</a> Text</i>
<br>
<br>
//all below to delete
<img src="todelete3.jpg">
<br><b>*<a href="http://nothing1.com" title="to delete4">to delete4</a></b>
<br><b>*<a href="http://nothing2.com" title="to delete5">to delete5</a></b>
<br><b>*<a href="http://nothing3.com" title="to delete6">to delete6</a></b>
</section>
&#13;
我有以下代码:
$ht = file_get_contents('http://localhost:8080/lesson/test17.html');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($ht);
$divs = $dom->getElementsByTagName('section');
foreach ($divs as $div)
{
if($div->getAttribute('id') == 'tresc')
{
$before = $dom->saveHTML($div);
$selector = new DOMXPath($before);
foreach($selector->query('//div[contains(attribute::class, "todelete1")]') as $e )
{
$e->parentNode->removeChild($e);
}
echo $before->saveHTML($before->documentElement);
}
}
如何移除以获取课程todelete1
,todelete2
和所有链接</i>
的div?
我不需要它!谢谢我帮忙。
答案 0 :(得分:0)
您的代码的主要误解是DOMXPath
init。您将<section id="tresc">
的HTML呈现为$before
变量,然后尝试实例化DOMXPath
传递$before
作为参数:DOMXPath
参数必须是{{3}但是,你传递一个字符串(呈现的HTML)。
要正确初始化DOMXPath
,您必须自己传递参数$dom
,并且可以在将HTML加载到$dom
对象之后启动它一次。无需为每个节点创建新的DOMXPath
。
侧面错误:当您在代码结束时尝试呈现<div>
的HTML时的语法。您使用以下语法:renderedHTLM->saveHTML( renderedHTML->documentElement )
而不是正确的语法:DOMDocumentObj->saveHTML( DOMElementContext )
。您必须在$dom->saveHTML( $div )
因此,您可以通过以下方式更改代码:
$ht = file_get_contents( 'http://localhost:8080/lesson/test17.html' );
$dom = new DOMDocument();
libxml_use_internal_errors( true );
$dom->loadHTML( $ht );
$selector = new DOMXPath( $dom ); // <----
$divs = $dom->getElementsByTagName('section');
foreach( $divs as $div )
{
if( $div->getAttribute( 'id' ) == 'tresc' )
{
foreach($selector->query( '//div[contains(attribute::class, "todelete1")]' ) as $e )
{
$e->parentNode->removeChild($e);
}
echo $dom->saveHTML( $div ); // <----
}
}
附注:如果要删除具有id
属性的元素,可以使用->getElementbyId
(id
在HTML页面中是唯一的),并以这种方式缩短代码:
$div = $dom->getElementById( 'tresc' );
$div->parentNode->removeChild( $div );
答案 1 :(得分:0)
thx @ fusion3k它的工作非常顺利。我删除了所有<div>
,但我最后一个问题是如何删除我知道的a
或img
代码选择器strip_tags
,但此函数会为我的示例留下文字
删除4,删除5,删除6
。
$divs = $dom->getElementsByTagName('section');
foreach( $divs as $div )
{
if( $div->getAttribute( 'id' ) == 'tresc' )
{
foreach($selector->query( '//div[contains(attribute::class, "todelete1")]' ) as $e )
{
$e->parentNode->removeChild($e);
}
foreach($selector->query( '//div[contains(attribute::class, "todelete2")]' ) as $f )
{
$f->parentNode->removeChild($f);
}
foreach($selector->query( '//a[contains(attribute::href,""]' ) as $g)
{
$g->parentNode->removeChild($g);
}
echo $dom->saveHTML( $div );
THX