You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP解析HTML:获取指定元素内部的子元素

Hey there! Parsing that HTML structure in PHP to pull out child elements is totally straightforward—let’s walk through the most reliable methods, since regex for HTML is a hard pass (you’ll hit messy edge cases faster than you can say "unclosed tag").

方法1:PHP原生DOMDocument + DOMXPath (推荐)

This is the go-to native solution because it properly handles HTML’s structure, even if your markup is a little non-standard (like your <div> directly containing <li> tags—DOM will auto-fix that without breaking your data).

Here’s a complete working example:

<?php
// 抑制解析警告(你的HTML里div直接包含li不符合规范,这行可以避免警告干扰)
libxml_use_internal_errors(true);

// 目标HTML内容
$html = '<div id="posts"> <li><a class="title" href="link1">Title 1</a><span class="hour">12:43</span></li> <li><a class="title" href="link2">Title 2</a><span class="hour">04:43</span></li> <li><a class="title" href="link3">Title 3</a><span class="hour">15:43</span></li> <li><a class="title" href="link4">Title 4</a><span class="hour">18:43</span></li></div>';

// 初始化DOM解析器
$dom = new DOMDocument();
$dom->loadHTML($html);

// 使用XPath实现灵活的元素定位
$xpath = new DOMXPath($dom);

// 获取#posts下的所有li元素
$listItems = $xpath->query('//div[@id="posts"]/li');

// 遍历每个列表项提取数据
foreach ($listItems as $item) {
    // 获取标题链接和文本
    $titleLink = $xpath->query('.//a[@class="title"]', $item)->item(0);
    $titleText = $titleLink->nodeValue;
    $titleHref = $titleLink->getAttribute('href');
    
    // 获取时间文本
    $hourSpan = $xpath->query('.//span[@class="hour"]', $item)->item(0);
    $hourText = $hourSpan->nodeValue;
    
    // 处理数据(输出、存入数据库等)
    echo "标题: {$titleText}, 链接: {$titleHref}, 时间: {$hourText}<br>";
}

// 清除解析错误缓存
libxml_clear_errors();
?>

简单说明:

  • libxml_use_internal_errors(true):屏蔽不规范HTML的解析警告,避免输出混乱。
  • XPath表达式//div[@id="posts"]/li可以精准定位目标元素,不用手动逐层遍历DOM节点。
  • 内部查询使用.//前缀,代表相对于当前<li>元素搜索,避免误抓其他区域的元素。
方法2:QueryPath(第三方库)

如果你更喜欢类似jQuery的简洁语法,QueryPath是不错的选择,它大幅简化了HTML的遍历和提取操作。首先通过Composer安装:

composer require querypath/querypath

使用示例:

<?php
require 'vendor/autoload.php';

$html = '<div id="posts"> <li><a class="title" href="link1">Title 1</a><span class="hour">12:43</span></li> <li><a class="title" href="link2">Title 2</a><span class="hour">04:43</span></li> <li><a class="title" href="link3">Title 3</a><span class="hour">15:43</span></li> <li><a class="title" href="link4">Title 4</a><span class="hour">18:43</span></li></div>';

// 将HTML加载到QueryPath中
$qp = htmlqp($html);

// 用类jQuery语法遍历并提取数据
$qp->find('#posts li')->each(function($listItem) {
    $title = $listItem->find('.title')->text();
    $link = $listItem->find('.title')->attr('href');
    $hour = $listItem->find('.hour')->text();
    
    echo "标题: {$title}, 链接: {$link}, 时间: {$hour}<br>";
});
?>

重要提醒:

绝对不要用正则表达式解析HTML! HTML结构灵活多变(嵌套标签、动态属性、空格差异等),正则无法可靠处理所有场景,一定要使用专业的HTML解析工具。

内容的提问来源于stack exchange,提问作者user3134277

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:04:59