I have search the entire internet, what I have found is getting values of tags with attributes. But I have a tag to extract contents, the tag has no attributes. The tag is cite
.
Here is my tag:
<div>
<cite>My name is Jimmy</cite>
<cite>My name is Paul</cite>
<cite>I am Sarah</cite>
</div>
And here is my DOM call:
$dom = new domDocument;
/*** load the html into the object ***/
@$dom->loadHTML($html);
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('cite');
/*** loop over the table rows ***/
foreach ($tables as $row) {
/*** get each column by tag name ***/
$cols = $row->getElementsByTagName('cite');
/*** echo the values ***/
echo $cols->textContent.'<br />';
}
I get this error:
Notice: Undefined property:
DOMNodeList::$textContent
What I would like to get is this:
My name is Jimmy
My name is Paul
I am Sarah
Please guys I need help. Thanks in advance!
http://php.net/manual/en/domdocument.getelementsbytagname.php
$html = "<div>
<cite>My name is Jimmy</cite>
<cite>My name is Paul</cite>
<cite>I am Sarah</cite>
</div>";
$dom = new domDocument;
$dom->loadHTML($html);
$cities = $dom->getElementsByTagName('cite');
foreach ($cities as $city) {
echo $city->nodeValue, PHP_EOL;
}
You currently trying to get the tag cite inside each tag cite. Simply remove the second getElementsByTagName
in your loop.
foreach ($tables as $row) {
echo $row->textContent.'<br />';
}