Здесь вам не нужно simple_html_dom
. Это можно сделать с помощью DOMDocument
и DOMXPath
. Оба являются частью ядра PHP.
Пример:
// your sample data
$html = <<
"Text to grab"
......
................
................
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"