0

I've got something like this:

$string = '<some code before><div class="abc">Something written here</div><some other code after>'

What I want is to get what is within the div and output it:

Something written here

How can I do that in php? Thanks in advance!

InfoArte
  • 13
  • 2

1 Answers1

0

You would use the DOMDocument class.

// HTML document stored in a string
$html = '<strong><div class="abc">Something written here</div></strong>';

// Load the HTML document
$dom = new DOMDocument();
$dom->loadHTML($html);

// Find div with class 'abc'
$xpath = new DOMXPath($dom);
$result = $xpath->query('//div[@class="abc"]');

// Echo the results...
if($result->length > 0) {
    foreach($result as $node) {
        echo $node->nodeValue,"\n";
    }
} else {
    echo "Empty result set\n";
}

Read up on the expression syntax for XPath to customize your DOM searches.

Anthony
  • 221
  • 2
  • 10