首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以下结构的PHP ->preg_match_all <h6>my标题</h6>一些文本... <h6>another标题</h6>更多文本

以下结构的PHP ->preg_match_all <h6>my标题</h6>一些文本... <h6>another标题</h6>更多文本
EN

Stack Overflow用户
提问于 2010-12-04 00:05:07
回答 2查看 358关注 0票数 0

我不顾一切地寻找解决方案来获得这个文本字符串

代码语言:javascript
运行
复制
<h6>First pane</h6>
... pane content ...
<h6>Second pane</h6>
Hi, this is a comment.
To delete a comment, just log in and view the post's comments.
There you will have the option to edit
or delete them.
<h6>Last pane</h6>
... last pane content ...

解析成PHP数组。

我需要把它分离出来

代码语言:javascript
运行
复制
1.
1.0=> First pane
1.1=> ... pane content ... 

2.
2.0=> Second pane
2.1=> Hi, this is a comment.
    To delete a comment, just log in and view the post's comments.
    There you will have the option to edit
    or delete them.

3.
3.0=> Last pane
3.1=> ... last pane content ...
EN

回答 2

Stack Overflow用户

发布于 2010-12-04 00:23:13

您的正则表达式应该如下所示:

代码语言:javascript
运行
复制
/<h6>([^<]+)<\/h6>([^<]+)/im

如果运行以下脚本,您将看到正在查找的值位于$matches1和$matches2中。

代码语言:javascript
运行
复制
$s = "<h6>First pane</h6>
... pane content ...
<h6>Second pane</h6>
Hi, this is a comment.
To delete a comment, just log in and view the post's comments.
There you will have the option to edit
or delete them.
<h6>Last pane</h6>
... last pane content ..";
$r = "/<h6>([^<]+)<\/h6>([^<]+)/im";

$matches = array();
preg_match_all($r,$s,$matches);

print_r($matches);
票数 1
EN

Stack Overflow用户

发布于 2010-12-04 00:24:20

您不应该尝试使用正则表达式来解析HTML。这注定会给除了最简单的HTML之外的所有人带来很多痛苦和不快,并且如果文档结构中有任何变化,它将立即崩溃。使用合适的超文本标记语言或DOM解析器,如php的DOMDocument http://php.net/manual/en/class.domdocument.php

例如,您可以使用getElementsByTagName http://www.php.net/manual/en/domdocument.getelementsbytagname.php获取所有h6

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4347372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档