如何获取HTML文档的文本节点总数?

Here is my code :

i want to get totall number of text node in HTML Document.

 // a new dom object
$dom = new domDocument;

// load the html into the object
$dom->loadHTMLFile('translated/test.html');

// discard white space
$dom->preserveWhiteSpace = false;

$i = 0;

// get elements by tagname body
while ($bodynodes = $dom->getElementsByTagName('body')->item($i)) {
    myFunc($bodynodes);
    $i++;
}

//var_dump($holder);

function myFunc($node) {
    static $i;
    if (!isset($i)) {
        $i = 0;
    }
    if ($node->childNodes) {
        foreach ($node->childNodes as $subNode):
            myFunc($subNode);
        endforeach;
    }else {

        if ($node->nodeType == 3 && trim($node->nodeValue) != ''):
            $i++;


        endif;
    }
    if ($node->lastChild):
        echo $i;
    endif;
}

But what i get is

Result ====>  1233

as there are only 3 text segment in my HTML document.

I think this should work to get the total:

$count = myFunc($dom->getElementsByTagName('body'));
echo "The document has $count text nodes
";

function myFunc($node) {
    if ($node->childNodes) {
        $total = 0;
        foreach ($node->childNodes as $subNode):
            $total+= myFunc($subNode);
        endforeach;
        return $total;
    }
    else
    {
        if ($node->nodeType == 3 && trim($node->nodeValue) != '') {
            return 1;
        } else {
            return 0;
        }
    }
}

What it does is call myFunc if the node has children treating those children as new trees and returning the number of text nodes toting up the sum. Otherwise it returns 1 or 0 depending on the node type and its value.