Question

I'm quite new to Simple HTML DOM Parser. I want to get a child element from the following HTML:

<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>

I'm trying to get the text "Text to grab"

So far I've tried the following query:

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

But it's not working. Any idea how to solve this ?

Was it helpful?

Solution 2

If it's always in the same place you can do:

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

OTHER TIPS

You don't need simple_html_dom here. It can be done with DOMDocument and DOMXPath. Both are part of the PHP core.

Example:

// 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);

Output:

"Text to grab"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top