如何找到第一个h3标签的内容? [英] How can I find the contents of the first h3 tag?

查看:173
本文介绍了如何找到第一个h3标签的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个正则表达式来查找第一个<h3>标记的内容.我在那里可以使用什么?

I am looking for a regex to find the contents of the first <h3> tag. What can I use there?

推荐答案

您应该使用php的DOM解析器而不是正则表达式.您正在寻找这样的内容(未经测试的代码警告):

You should use php's DOM parser instead of regular expressions. You're looking for something like this (untested code warning):

$domd = new DOMDocument();
libxml_use_internal_errors(true);
$domd->loadHTML($html_content);
libxml_use_internal_errors(false);

$domx = new DOMXPath($domd);
$items = $domx->query("//h3[position() = 1]");

echo $items->item(0)->textContent;

这篇关于如何找到第一个h3标签的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆