2014-04-01 1 views
1

나는 에 매우 익숙하다. 단순 HTML DOM 파서. 나는 다음과 같은 쿼리를 시도했습니다 지금까지간단한 HTML DOM 파서를 사용하여 텍스트 노드 검색

을 "텍스트 잡아"

<div class="article"> 
<div style="text-align:justify"> 
    <img src="image.jpg" title="image"> 
    <br> 
    <br> 
    "Text to grab" 
    <div>......</div> 
    <br></br> 
    ................ 
    ................ 
    </div> 
</div> 

내가 텍스트를 얻으려고 : 나는 다음과 같은 HTML에서 자식 요소를 얻으려면

$html->find('div[class=article] div')->children(3); 

하지만 작동하지 않습니다. 어떤 생각이 어떻게 해결할 수 있습니까?

+0

이 코드에 대한 몇 가지 상황을 가지고 도움이 될 것이다, 한 줄뿐 아니라 ... – celeriko

+0

어떤 오류가 있습니까? – mituw16

+0

어느 ** 아이 **에서 텍스트를 가져 오시겠습니까? – Aditya

답변

1

당신이 할 수 있습니다

$html->find('.article text', 4); 
3

여기에 simple_html_dom이 필요하지 않습니다. DOMDocumentDOMXPath으로 수행 할 수 있습니다. 둘 다 PHP 코어의 일부입니다.

예 :

// your sample data 
$html = <<<EOF 
<div class="article"> 
<div style="text-align:justify"> 
    <img src="image.jpg" title="image"> 
    <br> 
    <br> 
    "Text to grab" 
    <div>......</div> 
    <br></br> 
    ................ 
    ................ 
    </div> 
</div> 
EOF; 

// create a document from the above snippet 
// if you are loading from a remote url use: 
// $doc->load($url); 
$doc = new DOMDocument(); 
$doc->loadHTML($html); 

// initialize a XPath selector 
$selector = new DOMXPath($doc); 

// get the text node (also text elements in xml/html are nodes 
$query = '//div[@class="article"]/div/br[2]/following-sibling::text()[1]'; 
$textToGrab = $selector->query($query)->item(0); 

// remove newlines on start and end using trim() and output the text 
echo trim($textToGrab->nodeValue); 

출력 :이 같은 장소에 항상 경우

"Text to grab" 
+0

'여기에 simple_html_dom이 필요하지 않습니다. '나는 왜 그리고 어디에 사용해야하는지 궁금합니다. =) – Enissay

+0

@Enissay'simple_html_dom'은 PHP4 시대에 개발되었습니다. PHP4에는 XPath가 내장되어 있지 않습니다. – hek2mgl

+0

나는 이것이 내 자신에게 오랫동안 묻고있는 질문에 답했다. 그래서 "새로운"내장 된 PHP 돔이 완전히 그것을 대체 할 수 있는가? 그렇다면 여전히 널리 사용되는 이유에 대해 궁금합니다./ – Enissay

관련 문제