如何在phpquery中进行链接(几乎所有内容都可以是一个链) [英] How to chain in phpquery (almost everything can be a chain)

查看:131
本文介绍了如何在phpquery中进行链接(几乎所有内容都可以是一个链)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好, 我是phpquery的新手,这是我在stackoverflow上的第一篇文章,原因是我找不到phpquery链接的正确语法.我知道有人知道我在寻找什么.

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.

我只想删除div中的某个div.

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>

这几行代码很完美

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

但是当我尝试

$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?

谢谢!

推荐答案

仅当您尝试从div.node-links中删除该类时,第一行代码才有效,而不会删除该节点.

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();

它将输出:

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

至于第二行代码.我不确定为什么它不起作用,似乎您没有选择.node-links,但是我使用这些内容能够获得理想的结果.

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();

希望有帮助

这篇关于如何在phpquery中进行链接(几乎所有内容都可以是一个链)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆