simple_html_dom.php中的CSS类问题

时间:2014-08-09 08:11:55

标签: php html simple-html-dom

我有多个div,如下面的类

<div  class ="hub_categoryDeal Dubai  Healthcare BEAUTY HAIR_REMOVAL1 LASER_HAIR_REMOVAL Dubai"
<div  class ="hub_categoryDeal Dubai  Healthcare SERVICES HAIR_REMOVAL1 LASER_HAIR_REMOVAL Dubai"

类别名称如BEAUTYSERVICES在类中。如何在简单的html find()方法中获取它?

2 个答案:

答案 0 :(得分:0)

我不知道/无法为此找到正确的数组函数,但您可以获取所有类,然后过滤它们。像这样:

$html_string = '<div  class ="hub_categoryDeal Dubai  Healthcare BEAUTY HAIR_REMOVAL1 LASER_HAIR_REMOVAL Dubai"></div>"
<div  class ="hub_categoryDeal Dubai  Healthcare SERVICES HAIR_REMOVAL1 LASER_HAIR_REMOVAL Dubai"></div>';
$html = str_get_html($html_string);

$classes = array();
foreach ($html->find('div.hub_categoryDeal') as $element) {
    // push all the values inside the container
    $classes = array_merge($classes, array_filter(explode(' ', $element->class)));
}
$temp = array_count_values($classes); // check all the values
foreach($temp as $key => $count) {
    if($count > 1) { // if is repeated
        $found = array_keys($classes, $key); // get all the repeated thru their keys
        if($found) {
            foreach($found as $k) {
                unset($classes[$k]); // remove them
            }
        }
    }
}

echo '<pre>';
print_r($classes);

输出如:

Array
(
    [3] => BEAUTY
    [10] => SERVICES
)

答案 1 :(得分:0)

类属性中带有BEAUTY的元素:

$html->find('.BEAUTY')

class属性中包含SERVICES的元素:

$html->find('.SERVICES')