Php Domdocument How To Get That Content Of This Tag?
I am using domDocument hoping to parse this little html code. I am looking for a specific span tag with a specific id. Hello world
Solution 1:
You can use simple getElementById:
$dom->getElementById('CPHCenter_lblOperandName')->nodeValue
or in selector way:
$selector = new DOMXPath($dom);
$list = $selector->query('/html/body//span[@id="CPHCenter_lblOperandName"]');
echo($list->item(0)->nodeValue);
//or foreach($listas$span) {
$text = $span->nodeValue;
}
Solution 2:
Your four part question gets an answer in three parts:
- getElementsByTagName does not take an XPath expression, you need to give it a tag name;
- Nothing is output because no tag would ever match the tagname you provided (see #1);
- It looks like what you want is XPath, which means you need to create an XPath object - see the PHP docs for more;
Also, a better method of controlling the libxml errors is to use libxml_use_internal_errors(true) (rather than the '@' operator, which will also hide other, more legitimate errors). That would leave you with code that looks something like this:
<?php
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach($xpath->query("//span[@id='CPHCenter_lblOperandName']") as$node) {
echo$node->textContent;
}
Post a Comment for "Php Domdocument How To Get That Content Of This Tag?"