使getElementsByTagname作为标记上的数组返回

时间:2017-10-28 07:50:49

标签: php dom

嗨,任何人都可以想到这一点,我试图获取所有的html体内的所有文本,但返回已经组合所有文本的字符串而不是数组,但如果我指定它返回一个数组,但与其不工作因为我需要他们在阵列中进行数据操作,谢谢

$dom = new domDocument;
$content = $dom->getElementsByTagname('body');
/*** the array to return ***/
$out = array();
foreach ($content as $item)
{
    $out[] = $item->nodeValue;
}
return $out;

<html>
    <title> Food Wars</title>
    <body>
        <p>paragraph definition</p>
        <span> Super Wars </span>
        <b> This is a text </b>
        <div> Testing automatics </div>
       <h4>Toys</h4>
       <h4>Problem</h4>
       <h4>Solution</h4>
       <h4>Discussion</h4>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

使用strip_tags()函数$out[] = strip_tags($item->nodeValue);从字符串中删除html个标记