Fahad Khan
Fahad Khan

Reputation: 1635

Loop through html content and get titles and description - PHP

I am using str_get_html($string);

The content I am getting is:

> <div class="detail_sec">   <div class="news_post">   
> <h3>Some title</h3>    <p><span class="date_news">2 Jan
> 2015</span></p>    <p align="justify">Read More :: <a
> href="/news/2015/2-jan.pdf" target="_blank">PDF</a> | <a
> href="/news/2015/2-jan.jpg" target="_blank">JPG</a></p>   </div>
> </div>

<div class="detail_sec">
  <div class="news_post">
   <h3>Another title</h3>
   <p><span class="date_news">1 Jan 2015</span></p>
   <p align="justify">Read More :: <a href="/news/2015/1-jan.pdf" target="_blank">PDF</a> | <a href="/news/2015/2-jan.jpg" target="_blank">JPG</a></p>
  </div>
</div>
.
.
.

I want to loop through this content, and get arrays for h3, date_news class, and anchor tags.. I tried this, its working but only h3 (or any other at a time), I want all in single loop.

foreach ( $html->find("div[class=news_post] h3") as $h3) {
    $heading = trim($h3)->plaintext;
    $headingArr[]=$heading;
}
var_dump($headingArr);

Thanks for any help.

Upvotes: 0

Views: 397

Answers (1)

okdewit
okdewit

Reputation: 2566

You can use a comma as an AND operator when using the selectors, just like you'd do in CSS.

This for example would return an array of all h3s with div parent AND all spans with div parents:

$html->find("div h3, div span");

What you are trying to do seems like it should be done client-side in javascript (normally you generate HTML with PHP, you don't process it).

But there are good reasons to use DOM parsing, so if you are certain that you need to do this in PHP:

<?php 
include_once('simple_html_dom.php');

$string = '<div class="detail_sec"><div class="news_post"><h3>Another title</h3><p><span class="date_news">1 Jan 2015</span></p><p align="justify">Read More :: <a href="/news/2015/1-jan.pdf" target="_blank">PDF</a> | <a href="/news/2015/2-jan.jpg" target="_blank">JPG</a></p></div></div>';
$html=str_get_html($string);
$headingArr = array();

foreach ( $html->find("div[class=news_post] h3, div[class=news_post] span[class=date_news]") as $h3) {
    $heading = trim($h3->plaintext);
    $headingArr[]=$heading;
}
var_dump($headingArr);
?>

Upvotes: 2

Related Questions