使用html dom在CSS选择器中排除特定的类名

时间:2018-03-28 16:32:49

标签: html class parsing dom

我只想导入只包含class =“item-name”的文本 不和class =“item-name active” 您认为我如何申请进口?

HTML代码为:

<li class="list-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
    <a class="item" itemscope itemtype="http://schema.org/Thing" itemprop="item" href="/">
        <span class="item-name" itemprop="name">Category</span>
    </a>
</li>
<li class="list-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
    <a class="item" itemscope itemtype="http://schema.org/Thing" itemprop="item" href="/outlet">
        <span class="item-name" itemprop="name">Category 1</span>
    </a>
</li>
<li class="list-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
    <a class="item" itemscope itemtype="http://schema.org/Thing" itemprop="item" href="/outlet/fragrance">
        <span class="item-name" itemprop="name">Category 2</span>
    </a>
</li>
<li class="list-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
    <span class="active-item" itemscope itemtype="http://schema.org/Thing" itemprop="item">
        <span class="item-name active" itemprop="name">Title</span>
    </span>
</li>

我的PHP代码:

if ($html !== false) {
    $prod['id']=$html->find('span.code',0)->plaintext;
    $j=0;
    while (isset($html->find('span.item-name',$j)->plaintext)) {
        $cats[]=str_replace(['.',';'],' ',html_entity_decode($html->find('span.item-name',$j)->plaintext));
        $j++;
    }
    $prod['category']=implode(', ',$cats);
    unset($cats);
}

更新我修复

          if ($html !== false) {
              $prod['id']=$html->find('span.code',0)->plaintext;
              $j=0;
              while (isset($html->find('a.item',$j)->plaintext)) {
                 $cats[]=str_replace([' ','&#39',';'],' ',html_entity_decode($html->find('a.item',$j)->plaintext));
                 $new = preg_replace(['(\s+)u', '(^\s|\s$)u'], [' ', ''], $cats);
                 $j++;
              }
              $prod['category']=implode(', ',$new);
              unset($new);
              }

0 个答案:

没有答案