简体   繁体   中英

PHP simple dom parser select and remove multiple tags at once

I'm using PHP simple dom parser http://simplehtmldom.sourceforge.net/manual.htm . I'm able to remove a tag with a specific id by doing

$html = str_get_html('<div><div class="two">two</div></div>');     
$e = $html ->find('.two',0);
$e->outertext = '';
echo $html ->outertext;

Reading the simple dom api document, i see we can select multiple tags at once by including multiple classes or ids in the find function, but how can we remove all at once.

I tried the following but it doesn't work. It only removes the first class .two

$e = $html ->find('.two, .three, .four');
$e->outertext = '';
echo $html ->outertext;

Sorry for the confusion. As per the simple html dom parser documentation,

// Find all anchors and images with the "title" attribute
$ret = $html->find('a[title], img[title]');

<div class="mainnavs">
    <ul>
        <li>Tags</li>
        <li>Users</li>
    </ul>
</div>

<div class="askquestion">
    <ul>
        <li>
            Ask Questions
        </li>
    </ul>
</div> 

The above HTML I got the expected results,

$result = $html->find('[.mainnavs], [.askquestion]' , 0);

its works fine for me.

The issue i found in the above code is like your variable name. you content is in $html but you are using $str variable to find the div.

$html = str_get_html('<div><div class="two">two</div><div class="three">thomas</div><div    class="four">babu</div></div>');     
$e = $html->find('#two , #three',0);
$e->outertext = '';
echo $html->outertext;

Please check out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM