【问题标题】:how to find element in html using php?如何使用 php 在 html 中查找元素?
【发布时间】:2026-02-01 16:45:02
【问题描述】:

我正在尝试使用 php 在 html 文档中查找元素。

include_once('simple_html_dom.php');
            $handle = fopen("exercise/Exercise.php", "r");
            $file = fread($handle ,8192);
            $handle = file_get_html('exercise/Exercise.php');
            if (!empty($handle)) {
              $ret = $handle->find('div[id=abc]');
             }

但我不知道现在如何处理$ret?或者甚至检查它是否找到<div id="abc">?这是我不知道的正确方法吗,因为我不太擅长php?

编辑这个问题对我来说是非常广泛的理解。

【问题讨论】:

  • 回显$ret并找出答案
  • @Ghost 注意:C:\xampp\htdocs\csstutorial.php 中第 19 行数组中的数组到字符串转换
  • @PaulCrovella ` 你如何在 PHP 中解析和处理 HTML/XML? ` 它太远了,你能指出应该从哪里开始寻找吗?
  • 它应该是$handle->find('div[id=abc]', 0); 直接指向它,无论如何它是一个ID并且应该是唯一的
  • 您是否要进行实时搜索?还是只是文件中的数据?

标签: javascript php jquery html file-get-contents


【解决方案1】:

应该是这样的

 $handle->find('div[id=abc]', 0);

【讨论】: