2013-09-26 4 views
1

나는이 HTML있어 :특정 클래스와 DIV를 얻는 방법?

<div class="hello top">some content</div> 
<div class="hello top">some content</div> 
<div class="hello">some content</div> 
<div class="hello">some content</div> 
<div class="hello">some content</div> 

을 ... 그리고 난 클래스 "안녕하세요"가 아니라 클래스 "정상"(나는 단지 얻기 위해 3 개 마지막으로 된 div 원하는)가 만 된 div 얻기 위해 노력하고 있어요 .

Filter    Description 
[attribute]   Matches elements that have the specified attribute. 
[!attribute]   Matches elements that don't have the specified attribute. 
[attribute=value]  Matches elements that have the specified attribute with a certain value. 
[attribute!=value] Matches elements that don't have the specified attribute with a certain value. 
[attribute^=value] Matches elements that have the specified attribute and it starts with a certain value. 
[attribute$=value] Matches elements that have the specified attribute and it ends with a certain value. 
[attribute*=value] Matches elements that have the specified attribute and it contains a certain value. 

당신은 사용할 수 있습니다 :이 테이블 (Supports these operators in attribute selectors)에 따르면

foreach($html->find('div[class="hello"], div[class!="top"]') as $element) { 
    // some code... 
} 
+0

무엇이 문제입니까? –

+0

게시 된 코드가 작동하지 않았습니다. 나는 해결책을 찾는 것을 시도하고있다. 나는 이것에 대해 문서에서 아무 것도 발견하지 못했다. – Manny

+0

단순한 경우 만 처리 할 수 ​​있기 때문에 simple-html-dom이라고합니다. phpquery 또는 DOMXPath를 시도하십시오. – pguardiario

답변

0

:

내가 이렇게하지만 성공하지 뭔가를 시도

foreach($html->find('div[class$="hello"]') as $element) { 
    // some code... 
} 

하지만이 아니다 일치하기 때문에 신뢰할 수있는 솔루션 :

<div class="top hello"> 
0

이렇게하면 최신 "hello"클래스 이름을 이미 선택할 수 있습니다.

<html> 
    <header> 
    </header> 
    <body> 
     <?php 
     $html= ' 
     <div class="hello top">some content</div> 
     <div class="hello top">some content</div> 
     <div class="hello">ee some content</div> 
     <div class="hello">ee some content</div> 
     <div class="hello">ee some content</div>'; 

      $dom = new DomDocument(); 
      $dom->loadHTML($html); 
      $dom_xpath = new DOMXpath($dom); 
      $elements = $dom_xpath->query('//div[@class="hello"]'); 

      foreach($elements as $data){ 
       echo $data->getAttribute('class').'<br />'; 
      } 
     ?> 
    </body> 
</html> 
2

이 방법을 사용 :

var result = $("div:not(.top)"); 
console.log(result); 

가 // 당신은 "안녕하세요"클래스가 들어 있습니다 만 DIV를 얻을 것이다.

+0

Simple-html-dom! = jQuery – pguardiario

+0

서버 측에서 jQuery를 사용하려면 어떻게해야합니까? 그리고 결국, ".top"클래스없이 모든 div를 선택할 수 있지만 ".top"클래스없이 모든 div를 선택할 수없고 ".hello"클래스와 같은 시간에 선택할 수 없습니다. – Manny

0

[attribute $ = value] 지정된 속성을 가지며 특정 값으로 끝나는 요소를 찾습니다. 을 귀하의 경우에 사용하십시오.

foreach($html->find('div[class$="hello"]') as $element) { 
    // some code... 
} 
관련 문제