zoukankan      html  css  js  c++  java
  • PHP Simple HTML DOM Parser: check elements with multiple classes

    Get the information from this link:

    http://stackoverflow.com/questions/14264525/php-simple-html-dom-parser-select-only-divs-with-multiple-classes

    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

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

    or

    <divclass="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).

  • 相关阅读:
    生成一个平面矩形网格文件
    生成球 使用openMesh 库
    U盘安装可能会遇见UEFI的问题,使用easyBCD安装即可。
    vs 代码格式化
    跨域问题的出现和解决
    代理服务器的作用 和 推荐
    sublime user 配置
    git 大佬的相关配置
    win10 注册
    【BZOJ3566】—概率充电器(树形+概率dp)
  • 原文地址:https://www.cnblogs.com/liwp_Stephen/p/3538640.html
Copyright © 2011-2022 走看看