如何从字符串中提取 PHP 中的标题标签? [英] How to extract Heading tags in PHP from a string?

查看:41
本文介绍了如何从字符串中提取 PHP 中的标题标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从包含大量 HTML 的字符串中,如何将 <h1><h2>etc 标签中的所有文本提取到一个新变量中?

From a string that contains a lot of HTML, how can I extract all the text from <h1><h2>etc tags into a new variable?

我想从这些元素中捕获所有文本,并将它们作为逗号分隔值存储在一个新变量中.

I would like to capture all of the text from these elements and store them in a new variable as comma-delimited values.

是否可以使用preg_match_all()?

推荐答案

如果你真的想使用正则表达式,我认为:

If you actually want to use regular expressions, I think that:

preg_match_all('/<h[0-6]>([^</h[0-6]>*)</h/i', $string, $matches);

只要您的标题标签没有嵌套就应该可以工作.正如其他人所说,如果您无法控制 HTML,那么正则表达式不是实现此目的的好方法.

should work as long as your header tags are not nested. As others have said, if you're not in control of the HTML, regular expressions are not a great way to do this.

这篇关于如何从字符串中提取 PHP 中的标题标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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