Simple_html_dom:如何删除具有属性值的所有元素,第一个除外?
A few elements in my HTML page have the same value for the class attribute. I want to remove all of them (elements) except the first one.
I wrote the following SSCCE. So the question is
There are two loops being executed, the first one changes the attribute value for the first element and breaks the loop, and the second one then removes the elements with that attribute value.
So is there a shorter, less costly (in terms of memory, speed etc.) or more straightforward way to do this? May be can be done in a single loop or something like that? I feel I am making it unnecessarily long.
<?php
require_once("E:\\simple_html_dom.php");
$haystack = '<div>
<div class="removable" style="background-color:pink; width:100%; height:50px;">aa</div>
<div style="background-color:brown; width:100%; height:50px;">ss</div>
<div class="removable" style="background-color:grey; width:100%; height:50px;">dd</div>
<div class="removable" style="background-color:green; width:100%; height:50px;">gg</div>
<div style="background-color:blue; width:100%; height:50px;">hh</div>
<div class="removable" style="background-color:purple; width:100%; height:50px;">jj</div>
</div>';
$html_haystack = str_get_html($haystack);
//echo $html_haystack; //check
foreach ($html_haystack->find('div[class=removable]') as $removable) {
$removable->class='removable_first';
//$removable->style='background-color:black; width=100%; height=50px;'; //check
break;
}
foreach($html_haystack->find('div[class=removable]') as $removable) {
$removable->outertext= '';
}
$haystack = $html_haystack->save();
echo $haystack;
我的HTML页面中的一些元素具有相同的class属性值。 我想删除除第一个之外的所有元素(元素)。 p>
我编写了以下SSCCE。 所以问题是 p>
正在执行两个循环,第一个循环更改第一个元素的属性值并中断循环,第二个循环然后删除具有该属性值的元素 。 p>
那么是否有更短,更低成本(在内存,速度等方面)或更直接的方式来做到这一点? strong>可能可以做到 在一个循环或类似的东西? 我觉得我做的时间太长了。 p>
&lt;?php
require_once(“E:\\ simple_html_dom.php”);
$ haystack = '&lt; div&gt;
&lt; div class =“removable”style =“background-color:pink; width:100%; height:50px;”&gt; aa&lt; / div&gt;
&lt; div style =“background -color:brown; width:100%; height:50px;“&gt; ss&lt; / div&gt;
&lt; div class =”removable“style =”background-color:grey; width:100%; height:50px; “&gt; dd&lt; / div&gt;
&lt; div class =”removable“style =”background-color:green; width:100%; height:50px;“&gt; gg&lt; / div&gt;
&lt; div style =“background-color:blue; width:100%; height:50px;”&gt; hh&lt; / div&gt;
&lt; div class =“removable”style =“background-color:purple; width:100%; height :50px;“&gt; jj&lt; / div&gt;
&lt; / div&gt;';
$ html_haystack = str_get_html($ haystack);
// echo $ html_haystack; //检查
nforeach($ html_haystack-&gt; find('div [class = removable]')为$ removable){
$ removable-&gt; class ='removable_first';
// $ removable-&gt ;风格='背景色:黑色; 宽度= 100%; 高度= 50像素;'; // check
break;
}
foreach($ html_haystack-&gt; find('div [class = removable]')为$ removable){
$ removable-&gt; outertext ='';
}
$ haystack = $ html_haystack-&gt; save();
echo $ haystack;
code> pre>
div>
Find
function returns an array, so the first element has index 0
. No need then to use the 1st loop !
// Get all nodes
$array = $html_haystack->find('div[class=removable]');
// Edit the 1st => maybe you won't need this line if you're doing so only to skip the 1st node
$array[0]->class='removable_first';
// Remove the 1st from the array
unset($array[0]);
// Loop through the other nodes
foreach($array as $removable) {
$removable->outertext= '';
}
$html->find('.removable', 0)->class = 'removable_first';
foreach($html->find('.removable') as $removable){
$removable->outertext = '';
}