How to parse a list using simple html dom

// Test data
$input = <<<_DATA_
    <li id=xyz>
      John Johnson
    <sup>1<sup>
    ","
    </li>
_DATA_;

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($input);

// >> Long answer
echo "Long answer:<br/>";

// Search all text nodes inside the target node
$search = $html->find('li#xyz text');

// Loop through each node and print it
foreach( $search as $i => $txt ) {
    // No need to specify "->plaintext" since the content is already in plain text here
    echo "$i => " . $txt->plaintext . "<br/>";
}

// >> Short answer
echo "<hr>";
echo "Short answer:<br/>";

// Specifying the index (0th here) returns the Nth element from the array containing all search results
echo $html->find('li#xyz text', 0)->plaintext;

// Clear DOM object
$html->clear();
unset($html);
Mới hơn Cũ hơn