0

I am trying to use the code bellow to fetch some posts from my blog but it's a little difficult.

include('simple_html_dom.php');
$html->clear();
getArticles('http://www.example.com');

function getArticles($page) {
    global $articles, $descriptions;

    $html = new simple_html_dom();
    $html->load_file($page);

    $items = $html->find('li');  

    foreach($items as $post) {
        # remember comments count as nodes
        $articles[] = array($post->children(1)->innertext);
    }

My html structure is:

<li class="post-1840 post type-post status-publish format-standard hentry category-perierga">

<a href="http://www.example.com/?p=1840" title="my post title">
<img width="200" height="179" src="images/iko-200x179.jpg" class="attachment-archive wp-post-image" alt="iko-200x179.jpg" title=""></a>

<h2 class="leading"><a href="http://www.example.com/?p=1840">My Post Title!</a></h2>

<p class="widgetmeta sserif">
6 hours ago  | 
<a href="http://www.example.com/?cat=2" title="View all posts in Weird" rel="category">Weird</a> | 
<a href="http://www.example.com/?author=1" title="Posts by adminadmin" rel="author">admin</a> | 
<span>Comments Off</span>                      
</p>
<p class="teaser">my shord post description...</p>
<a class="mainbutton fr" href="http://www.example.com/?p=1840">Read More »</a>

</li>

I want to fetch the img, the (that is y post title), that holds the short description of my post.

Thank you for one one more time guys!!!

4

1 回答 1

1

如果我理解得很好,那么很容易获得所有这些信息......方法如下:

// includes Simple HTML DOM Parser
include "simple_html_dom.php";

// The input
$text = '<li class="post-1840 post type-post status-publish format-standard hentry category-perierga">

<a href="http://www.example.com/?p=1840" title="my post title">
<img width="200" height="179" src="images/iko-200x179.jpg" class="attachment-archive wp-post-image" alt="iko-200x179.jpg" title=""></a>

<h2 class="leading"><a href="http://www.example.com/?p=1840">My Post Title!</a></h2>

<p class="widgetmeta sserif">
6 hours ago  | 
<a href="http://www.example.com/?cat=2" title="View all posts in Weird" rel="category">Weird</a> | 
<a href="http://www.example.com/?author=1" title="Posts by adminadmin" rel="author">admin</a> | 
<span>Comments Off</span>                      
</p>
<p class="teaser">my shord post description...</p>
<a class="mainbutton fr" href="http://www.example.com/?p=1840">Read More »</a>

</li>';

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($text);


// Find all li elements
$items = $html->find('li');  

// loop into each li element
foreach($items as $i => $post) {
    // get the img
    $img = $post->find('img', 0)->src;

    // get the post's url       
    $url = $post->find('a', 0)->href;

    // get the title
    $title = $post->find('a', 0)->title;

    // another way to get the title
    $title2 = $post->find('h2.leading', 0)->plaintext;

    // get the description
    $desc = $post->find('p.teaser', 0)->plaintext;

    // Print all
    echo "\n$i => $img | $url | $title | $title2 | $desc";
    echo "<hr/>";
}

// Clear dom object
$html->clear(); 
unset($html);

OUTPUT:
=======
0 => images/iko-200x179.jpg | http://www.example.com/?p=1840 | my post title | My Post Title! | my shord post description...

现场演示

于 2013-11-13T16:01:53.853 回答