How to chain in phpquery (almost everything can be a chain)

旧街凉风 提交于 2019-12-12 00:22:56

问题


Good day everyone, I'm very new with phpquery and this is my first post here at stackoverflow for a reason that i cant find the correct for syntax for the phpquery chaining. I know someone knows what i been looking for.

I only want to remove the a certain div inside a div.

 <div id = "content"> 
        <p>The text that i want to display</p>
        <div class="node-links">Stuff i want to remove</div>
 </content>

This few lines of codes works perfect

 pq('div.node-links')->remove();
 $text = pq('div#content');
 print $text; //output: The text that i want to display

But when I tried

$text = pq('div#content')->removeClass('div.node-links'); //or
$text = pq('div#content')->remove('div.node-links');

 //output: The text that i want to display (+) Stuff i want to remove

Can someone tell me why the second block of code is not working?

Thanks!


回答1:


The first line of code will only work if your trying to remove the class from div.node-links, it won't remove the node.

If you are trying to remove the class you need to change it from:

$text = pq('div#content')->removeClass('div.node-links');
// to
$text = pq('div#content')->find('.node-links')->removeClass('node-links')->end();

which will output:

<div id="content">
    <p>The text that i want to display</p>
    <div>Stuff i want to remove</div>
</div>

As for the second line of code.. I'm not exactly sure why it is not working, it seems like your not selecting .node-links but I was able to get the desired results using these.

// $markup = file_get_contents('test.html');
// $doc = phpQuery::newDocumentHTML($markup);
$text = $doc->find('div#content')->children()->remove('.node-links')->end();

// or 

$text = pq('div#content')->find('.node-links')->remove()->end();

// or 

$text = pq('div#content > *')->remove('.node-links')->parent();

Hope that helps




回答2:


Since remove() does not take any parameter, you can do:

$text = pq('div#content div.node-links')->remove();


来源:https://stackoverflow.com/questions/21183186/how-to-chain-in-phpquery-almost-everything-can-be-a-chain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!