2
2
2chdotru2013-12-04 16:06:49
PHP
2chdotru, 2013-12-04 16:06:49

How to parse a page with PHP Simple HTML DOM Parser?

Just started learning and can't figure it out.
There is a page from which you need to get the following:
From the post-contenedor diva Post
name - qsdfgh Inserted
image - iiimgur.com/HRrtdzw.gif
Tags - gol - hgg - ghf - hjj
Votes (Puntos) - 10
Favorites (FAVORITOS) - 1
Date post creation - 06/24/2013
ps Page taken from Google by request qsdfgh

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2013-12-04
@2chdotru

Try phpQuery

<?php 
include 'phpQuery.php';
$html = file_get_contents('http://demo.phpost.net/posts/arte/57/qsdfgh.html');
phpQuery::newDocumentHTML($html, $charset = 'utf-8');

$result = array();
$result['title'] = pq('div.post-title')->find('h1')->text();

$result['img'] = array();
foreach (pq('div.post-contenido')->find('img') as $img) {
  $result['img'][] = pq($img)->attr('src');
}

$result['tags'] = array();
foreach (pq('div.tags-block')->find('a[rel=tag]') as $a) {
  $result['tags'][] = pq($a)->text();
}

print_r($result);

In response
Array
(
    [title] => qsdfgh
    [img] => Array
        (
            [0] => http://i.i.imgur.com/HRrtdzw.gif
        )

    [tags] => Array
        (
            [0] => gol
            [1] => hgg
            [2] => ghf
            [3] => hjj
        )

)

A
Alexey Sundukov, 2013-12-04
@alekciy

XPath .

S
sasha, 2013-12-05
@madmages

habrahabr.ru/post/110112/ don't use the symbol, it's clean and memory leaks happen, but nokogiri is an optimal and simple thing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question