2016-09-16 4 views
1

나는 Get DIV content from external Website의 솔루션을 좋아하지만 특정 문자열이 들어있는 DIV 클래스에서 값을 찾아야합니다. 내 경우에는 btx764839 (예 :)입니다. div 클래스의 이름은 문자열 자체가 아니지만 예를 들어 <div class="dark btx764839">76</div>입니다.특정 문자열로 DIV 내용 가져 오기

btx764839을 포함하는 DIV를 검색하고 그 특정 것을 참조하는 스크립트가 필요합니다. 나는 성공하지 않고 과 많은 다른 것들을 시도해 왔습니다. 다른 게시물에서 찾은 스크립트 아래에 (내 조정없이). 나는 약간의 도움에 감사 할 것입니다. 고마워요!

$url = 'url'; 
$content = file_get_contents($url); 
$first_step = explode('<div class="dark btx764839">' , $content); 
$second_step = explode("</div>" , $first_step[1]); 

echo $second_step[0]; 

이 내 최신 시도 : 제공된 예에서 가져온

$url = 'url'; 
$content = file_get_contents($url); 

foreach($content as $div) { 
    // Loop through the DIVs looking for one withan id of "content" 
    // Then echo out its contents (pardon the pun) 

    // get class attribute of a div, it's a string 
    $class = $div->getAttribute('class'); 

    // find substring 'btx764839' in $class 
    if (strpos($class, 'btx764839') !== false) { 
     echo $div->nodeValue; 
     } 
} 

답변

0

코드 :

foreach($divs as $div) { 
    // Loop through the DIVs looking for one withan id of "content" 
    // Then echo out its contents (pardon the pun) 

    // get class attribute of a div, it's a string 
    $class = $div->getAttribute('class'); 

    // find substring 'btx764839' in $class 
    if (strpos($class, 'btx764839') !== false) { 
     echo $div->nodeValue; 
    } 
} 
+0

미안 해요, 난 어떻게 내와 코드를 결합하는 아무 생각이 없습니다. 이 코드는 다른 페이지'$ url = 'url';'의 내용을 참조합니다. – vloryan