I have the following code snippet I’m trying to get the text from:
<span class="article-title1"><div id="user-sub-summary">Love to get text</div></span>
<div id="user-sub-intro"><p>this is my intro paragraph</p></div>
I’m trying to get just the text in the with the id “user-sub-summary”
Ultimately I’m going to get this text and display it in an editable textbox so people can change this if they would like.
my existing code looks like this ($intro = the html snippet above):
$dom = new DOMDocument;
$dom->loadHTML($intro);
var_dump($dom->getElementById('user-sub-summary'));
This just returns ‘NULL’ on the page – but I can’t figure out why. I’ve tried searching here, I’ve googled, I’ve looked everywhere to try and figure this out but have come up empty handed.
You need to declare a doctype.
http://www.php.net/manual/en/domdocument.getelementbyid.php#100402
And it’s always a good practice to validate your data before parsing.
$dom->validateOnParse = true;