使用 PHP PCRE 获取 div 内容

发布于 2024-08-03 15:26:13 字数 356 浏览 3 评论 0原文


我正在尝试使用 PHP 的 PCRE 从 div 中获取数据(基于他的 id)。目标是根据 div 的 id 获取其内容,并使用递归/深度来获取其中的所有内容。这里的主要问题是在“主 div”内获取其他 div,因为正则表达式一旦获取在初始 < 之后找到的下一个 就会停止强>



我尝试了很多不同的方法来解决这个问题,但没有一个有效。在我看来,最好的解决方案是使用 R 参数(递归),但从未让它正常工作。

有什么想法吗?

预先感谢:D

I'm trying to fetch data from a div (based on his id), using PHP's PCRE. The goal is to fetch div's contents based on his id, and using recursivity / depth to get everything inside it. The main problem here is to get other divs inside the "main div", because regex would stop once it gets the next </div> it finds after the initial <div id="test">.

I've tryed so many different approaches to the subject, and none of it worked. The best solution, in my oppinion, is to use the R parameter (Recursion), but never got it to work properly.

Any Ideais?

Thanks in advance :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-08-10 15:26:13

你最好使用某种形式的 DOM 解析器 - 正则表达式确实不适合这个问题。如果你想要的只是基本的 HTML dom 解析,像 simplehtmldom 这样的东西就很适合你。安装起来很简单(只需包含一个 PHP 文件),使用起来也很简单(2-3 行就可以满足您的需要)。

include('simple-html-dom.php');

$dom = str_get_html($bunchofhtmlcode);
$testdiv = $dom->find('div#test',0); // 0 for the first occurrence
$testdiv_contents = $testdiv->innertext;

You'd be much better off using some form of DOM parser - regex really isn't suited to this problem. If all you want is basic HTML dom parsing, something like simplehtmldom would be right up your alley. It's trivial to install (just include a single PHP file) and trivial to use (2-3 lines will do what you need).

include('simple-html-dom.php');

$dom = str_get_html($bunchofhtmlcode);
$testdiv = $dom->find('div#test',0); // 0 for the first occurrence
$testdiv_contents = $testdiv->innertext;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文