PHPverse 2025

Voting

The Note You're Voting On

naudyj at aus3d.com
17 years ago
The following can take a XML_TEXT_NODE node and return the contents in an array. Yanick's contribution rocks - but
it overwrote with duplicates only keeping the last line
in the returned array. All the other functions i tested from various sources failed to handle text nodes correctly. Hope this helps someone. It is adapted from code on this site.

function myTextNode($n, &$a)
{
static $depth = 0;
static $sz = '';

if ($cn = $n->firstChild)
{
while ($cn)
{
if ($cn->nodeType == XML_TEXT_NODE)
{
$sz .= $cn->nodeValue;
}
elseif ($cn->nodeType == XML_ELEMENT_NODE)
{
$b = 1;
if ($cn->hasChildNodes())
{
$depth++;
if ($this->myHeadings($cn, $a))
{
if ($sz){
array_push($a, $sz);
$sz = '';
}
}
$depth--;
}
}
$cn = $cn->nextSibling;
}
return $b;
}
}

so you could use:

$nodes = $dom->getElementsByTagName("td");
if($nodes){
foreach ($nodes as $node){
$a = Array();
myTextNode($node, $a);
}
}

<< Back to user notes page

To Top