PHP简单HTML DOM解析器:只选择具有多个类的DIV [英] PHP Simple HTML DOM Parser: Select only DIVs with multiple classes

查看:103
本文介绍了PHP简单HTML DOM解析器:只选择具有多个类的DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找疯狂,没有找到解决办法。问题很简单。



假设我有3个DIV:

 < div class =class1> 
< div class =subclass> TEXT1< / div>
< / div>

< div class =class2>
< div class =subclass> TEXT2< / div>
< / div>

< div class =class1 class2>
< div class =subclass> TEXT3< / div>
< / div>

所以,很简单。我只想找到TEXT3,它有BOTH class1和class2。使用简单的HTML DOM解析器,我似乎不能让它工作。



这是我尝试:

  foreach($ html-> find([class = class1],[class = class2])as $ item){
$ items [] = - > find('。subclass',0) - > plaintext;
}

问题是,用

  find([class = class1],[class = class2])

$ b b

它找到了所有的,因为逗号是一个OR,如果我离开逗号,它在class1里面寻找嵌套的class2。我只是在寻找AND ...



EDIT



19greg96我发现

  div [class = class1 class2] 

工作,问题是,它寻找那两个顺序。假设我有

 < div class =class1 class2> 
< div class =subclass> TEXT3< / div>
< / div>

,如果我有

 < div class =class1 class2 class3> 
< div class =subclass> TEXT3< / div>
< / div>

它会工作,当我把一个asterix,因为它寻找子字符串:

  div [class * = class1 class2] 



我知道class1和class3是存在的,这仍然不工作。任何想法如何只是寻找A& B在任何随机顺序?

  div [class = class1 class3] 
解决方案

简单答案:



< b
$ b

  find(。class1.class2)

这将查找具有class1和class2的任何类型的元素(div,img,a等..)。如果要指定要匹配的元素的类型,请将其添加到开头而不带如:

  find(div.class1.class2)

在两个指定的类之间的空格,它将元素与嵌套在元素中的类或元素匹配第一个类:

  find(。class1 .class2)

会匹配

 < div class =class1> 
< div class =class2>将返回< / div>
< / div>

 < div class =class1 class2>这将返回< / div> 

编辑:
我试过你的代码,发现上面的解决方案不工作。
然而,工作的解决方案如下:

  $ html-> find(div [class = class1 class2])

EDIT2:
这是dom解析器中的一个错误,没有简单的方法这样做。
解决方案我可以想到:

  $ find = $ html-> find(。class1); 
$ ret = array();
foreach($ find as $ element){
if(strpos($ element-> class,'class3')!== false){
$ ret [] = $ element;
}
}
$ find = $ ret;

基本上你发现所有的元素都有类一个迭代通过这些元素找到那些有类两个(在这种情况下为三个)。


I was searching like mad and found no solution. The problem is simple.

Let's say I have 3 DIVs:

<div class="class1">
  <div class="subclass"> TEXT1 </div>
</div>

<div class="class2">
  <div class="subclass"> TEXT2 </div>
</div>

<div class="class1 class2">
  <div class="subclass"> TEXT3 </div>
</div>

So, very simple. I just want to find the TEXT3, which has BOTH class1 and class2. Using Simple HTML DOM Parser, I can't seem to get it to work.

Here's what I tried:

foreach($html->find("[class=class1], [class=class2]") as $item) {
$items[] =  $item->find('.subclass', 0)->plaintext;
}

The problem is, with

find("[class=class1], [class=class2]")

it's finding all of them, as the comma is like an OR, if I leave the comma, it's looking for nested class2 inside class1. I am just looking for an AND...

EDIT

Thanks to 19greg96 I found out that

div[class=class1 class2]

works, the problem is that it looks for exactly those two in that order. Let's say I have

<div class="class1 class2">
  <div class="subclass"> TEXT3 </div>
</div>

then it works, and if I have

<div class="class1 class2 class3">
  <div class="subclass"> TEXT3 </div>
</div>

it works when I put an asterix, as it looks for the substring:

div[class*=class1 class2]

PROBLEM

I know only that class1 and class3 is there, but maybe others and in random order. That still doesn't work. Any idea how to just look for A & B in any random order? So that

div[class=class1 class3]

works with that example?

解决方案

Simple answer:

find(".class1.class2")

this will look for any type of element (div,img,a etc..) that has both class1 and class2. If you want to specify the type of element to match add it to the beginning without a . like:

find("div.class1.class2")

If you have a space between the two specified classes it will match elements with both the classes or elements nested in the element with the first class:

find(".class1 .class2")

will match

<div class="class1">
  <div class="class2">this will be returned</div>
</div>

or

<div class="class1 class2">this will be returned</div>

edit: I tried your code and found that the solutions above do not work. The solution that does work however is as follows:

$html->find("div[class=class1 class2]")

EDIT2: As this is a bug in the dom parser, there is no simple way of doing this. Solution I could think of:

$find = $html->find(".class1");
$ret = array();
foreach ($find as $element) {
    if (strpos($element->class, 'class3') !== false) {
        $ret[] = $element;
    }
}
$find = $ret;

basically you find all the elements with class one than iterate through those elements to find the ones that have class two (in this case three).

这篇关于PHP简单HTML DOM解析器:只选择具有多个类的DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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