简体   繁体   English

PHP HTML DOM从标签中提取文本,而嵌套标签中不包含文本

[英]PHP HTML DOM extracting text from a tag without including text in nested tags

I have html in the following format 我有以下格式的html

<html>
    <body>
        <div>
            Hello
            <span>world1
            </span> 
            <span>world2
            </span> 
            <span>world3
            </span> 
        </div>
        <div>
            Hello
            <span>world1
            </span> 
            <span>world2
            </span> 
            <span>world3
            </span> 
        </div>
    </body>
</html>

I need to extract data from the div without including the text inside the span..and store it in a variable and extract the text inside the third span inside each div in another variable. 我需要从div中提取数据而不在span内包含文本..并将其存储在变量中,然后在每个div内的第三个span内将文本提取到另一个变量中。 ie. 即。 in the first variable I need Hello. 在第一个变量中,我需要Hello。 And in the second variable I need world3. 在第二个变量中,我需要world3。 How to do that . 怎么做 。 The nodeValue property of each div returns text including that inside spans .How to avoid that 每个div的nodeValue属性返回包含span内的文本。如何避免这种情况

遍历div的childNodes并从所有textNodes的childNodes(它们的nodeType3 )的nodeValues构造一个字符串。

I think you are looking for something like this. 我认为您正在寻找类似的东西。 fiddle 小提琴

document.getElementsByTagName("div")[0].childNodes[0].textContent;
document.getElementsByTagName("div")[0].childNodes[5].textContent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM