使用php dom
问题我们需要获得以下输出:; $ content = ['这是第1章','这是第2章','这是第3章'];
solution将HTML加载到domdocument对象:
使用parsetoArray()函数从指定类中提取元素中的文本:通过匹配的节点迭代并提取其文本内容。将提取的文本存储在返回的数组中。
这是完整的php代码:QUERY($ XPATHQUERY); $ resultArray = []; foreach($ elements as $ element){ $ nodes = $ element-> childnodes; foreach($ nodes as $ node){ $ resultArray [] = $ node-> nodeValue; } } 返回$ resultarray; } $ test = 第1章
这是第1章
第2章
这是第2章
第3章
这是第3章
html; $ dom = new domdocument(); $ dom-> loadhtml($ test); $ xPath = new domxpath($ dom); $ heading = parsetoArray($ xpath,'heading1-h'); $ content = parsetoArray($ xPath,'normal-h'); var_dump($ heading); 回声“免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3