Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
没有子元素的元素的PHP DOMDocument值_Php_Html_Domdocument - Fatal编程技术网

没有子元素的元素的PHP DOMDocument值

没有子元素的元素的PHP DOMDocument值,php,html,domdocument,Php,Html,Domdocument,我得到一个DOM文档,它看起来像这样: <font size="6" face="Arial"> CONTENT <font size="5" face="Arial">...</font> <br> <table cellspacing="1" cellpadding="1" border="3" bgcolor="#E7E7E7" rules="all">...</table> <t

我得到一个DOM文档,它看起来像这样:

<font size="6" face="Arial">
CONTENT
    <font size="5" face="Arial">...</font>
    <br>
    <table cellspacing="1" cellpadding="1" border="3" bgcolor="#E7E7E7" rules="all">...</table>
    <table cellspacing="1" cellpadding="1">...</table>
    <font size="3" face="Arial" color="#000000">...</font>
</font>

内容
...

... ... ...
现在我只想获取内容,而不是所有其他子元素


我该怎么做呢?

您可以抓取第一个节点,它是第一个
标记的子节点

// Get the first <font> tag
$font = $doc->getElementsByTagName( 'font')->item(0);

// Find the first DOMText element
$first_text = null;
foreach( $font->childNodes as $child) {
    if( $child->nodeType === XML_TEXT_NODE) {
        $first_text = $child; 
        break;
    }
}

if( $first_text != null) {
    echo 'OUTPUT: ' . $first_text->textContent;
}
较短:

$output = $xml->getElementsByTagName("font")->item(1)firstChild->textContent;

nickb的解决方案也很有效,如果内容在其中一个子孩子之后,效果会更好。但是,在我的例子中,由于它不能做到这一点,所以这一个更短。

IIRC,在有效的HTML中,您不能在
中嵌套
。如何获取Domdocument???正在尝试在服务器端或客户端获取结果…可能是,代码是由第三方程序生成的,我必须对其进行分析。font???????????????????克服它!!!它已被弃用
$output = $xml->getElementsByTagName("font")->item(1)firstChild->textContent;