php - DOM 解析 - 如何解析这样的文件中的 html

标签 php html parsing dom

我想从 dom 中提取从带有 curl 请求的外部服务器获得的文件的文本。我将请求放在变量 calle $html_response 中。

我开始了

$dom = new DOMDocument;
$dom->loadHTML($html_response);

但是我将如何提取该文件末尾的文本? (向下滚动显示)
<html>
<head>
  <title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<font style="text-decoration:none; font-family: Arial; font-size: 40px; color: #b4b4b4; eight: 35px;">HI</font>
<div class="toolbar">
</div>
<style type="text/css">
body,
td,
th {
  color: #000000;
}
body {
  background-color: #eeeeee;
}
</style>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<form method="post" action="">
<p><b>YES YOU CAN</b>
  <input type="text" size="12" maxlength="14" name="named" value="">
  <input type="submit" value="a" name="submit"><br>
</p>
<p><span class="center"><b>PT1</b></span></p>
<p><span class="center"><b>PT2</b> <br>
  <b>XXXXXXXX bold</b> other words!</span></p>
</form><br> other words
<font size="5" face="monospace" color="Black">other words</font>
<br><br>


CANT' TAKE THIS PART BECOUSE THERE ISN'T A TAG THAT CLOSE THIS TEXT
AND I'M WORKING ON A EXTERNAL WEBSITE


</body>
</html>

谢谢!

最佳答案

您可以通过使用像这样的 DOM php 解析器来为您的隐匿体提供响应文本:

$dom = new DOMDocument;
$dom->loadHTML($html_response);

// retrieve text node at 'body' level with XPath
$xpath = new DOMXpath($dom);
$textNodes = $xpath->query('/html/body/child::text()');

// filter the nodes' content to retrieve the most pertinent ones (here, remove empty texts)
$texts = array();
foreach($textNodes as $node)
{
    if( strlen($node->nodeValue) > 0)
        $texts[] = $node->nodeValue ;
}

// get the latest text, as what you need is always at the bottom of the page 
echo end($texts); // CANT' TAKE THIS PART BECOUSE THERE ISN'T A TAG THAT CLOSE THIS TEXT AND I'M WORKING ON A EXTERNAL WEBSITE 

或者如果有 2 <br><br> ,则简单地分解代码:
$whatryousearchingfor = explode('<br><br>',$html_response)[1];

良好的编码 2 U

关于php - DOM 解析 - 如何解析这样的文件中的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54962364/

相关文章:

java - 有没有一个可以用 Java 编写 html 的库?

c# - 在 C# 中解析命令行参数/选项

PHP : Best way to push data from server to clients. ....?

php - 如何知道用户何时关闭浏览器?聊天应用

PHP 排名和更新所有数据库行

php - 使用Mysql在php循环中获取数组时出现错误

javascript - OnClick 事件,将文本添加到 div,将其删除(如果存在),并按描述名称进行分组

html - 包含可滚动预置时,表格单元格无法正确调整大小

java - 当我尝试使用 BufferedReader 将整数输入存储到数组中时遇到数字格式异常

Dart 中的 XML 绑定(bind)