1

I have HTML structure like this

 <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>

I have one div element - comments which contains many other div elements- comment. I need to get header element, which contains comment creator name, and *content, which contains the comment. I have code in PHP like this:

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 />';
                            }
 }

But with this code I can't get correct order, like comment author name and corresponding comment and so on. How to do this?

Thanks!

dreamPr
  • 321
  • 1
  • 2
  • 14

1 Answers1

0

If you don't like to use DOM (and don't want to use Regex with HTML), you can try to explode the HTML text with <div class="article-comment-header"> string.

The very first element (index 0) of the result array will be useless (it's all before the first <div>, so start looping from the second (index 1).

Then explode the 2nd element with </div>. The very first element of the second array is the header.

Then do the something similiar to get the author.

Hint: PHP explode() function provides additional argument $limit allowing you to separate text to only two pieces.

I agree this is not the best solution, but I think quite simple. I'd rather use some XML class to find it.

Voitcus
  • 4,463
  • 4
  • 24
  • 40