Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP解析HTML并使用cycle获取值_Php_Parsing - Fatal编程技术网

使用PHP解析HTML并使用cycle获取值

使用PHP解析HTML并使用cycle获取值,php,parsing,Php,Parsing,我有这样的HTML结构 <div class = article-comments> <div class="article-comment"> <div class="article-comment-header">...</div> <div class="article-comment-content">...</div> </div> <div class="art

我有这样的HTML结构

 <div class = article-comments>
  <div class="article-comment">
     <div class="article-comment-header">...</div>
     <div class="article-comment-content">...</div>
  </div>
  <div class="article-comment">
     <div class="article-comment-header">...</div>
     <div class="article-comment-content">...</div>
  </div>
</div>
.
.
.
</div>

...
...
...
...
.
.
.
我有一个div元素comments,它包含许多其他div元素comment。我需要获取header元素,其中包含注释创建者名称,以及*content,其中包含注释。我在PHP中有如下代码:

foreach($bot->parseBetweenRegexArray($data, '<div.*class="article-comment-content">', '<\/div>') as $commentary ){ 

   printf("comment: %s",$commentary); 

foreach($bot->parseBetweenRegexArray($data, '<div.*class="article-comment-header">', '<\/div>') as $name)  {

   printf("name: %s",$name); '<br />';
                            }
 }
foreach($bot->parseBetweenRegexArray($data,,'')作为$commentation){
printf(“注释:%s”,注释$s);
foreach($bot->parseBetweenRegexArray($data,,'')作为$name){
printf(“名称:%s”,$name);“
”; } }
但是使用这段代码我无法获得正确的顺序,比如注释作者姓名和相应的注释等等。 如何做到这一点


谢谢

如果您不喜欢使用DOM(也不想将正则表达式与HTML一起使用),可以尝试使用
字符串
分解
HTML文本

结果数组的第一个元素(索引0)将无效(都在第一个
之前),因此从第二个元素(索引1)开始循环

然后用
分解第二个元素。第二个数组的第一个元素是头

然后做一些类似的事情来得到作者

提示:PHP函数提供了额外的参数
$limit
,允许您将文本分成两部分


我同意这不是最好的解决方案,但我认为非常简单。我宁愿使用一些XML类来找到它。

你不能用正则表达式解析HTML:如果是这样,那么,我如何使用cycle正确获取这些HTML div属性的值呢?我不知道cycle,但我会寻找DOM或SAX解析器。foreach($HTML->find('div.article-comments'))作为$article){?}…….如何使用DOM解析器实现这一点?