Bonjour,

Voilà je cherche tout les titres dans une page pour ensuite les stocker dans un tableau pour ensuite les afficher comme je le désire.
Mais le tableau ce remplit avec n'importe quoi.

Voici mon code:

$html = new simple_html_dom();
        $html = file_get_html('http://www.monsite.fr/fichier.html');

        $ret = $html->find('.purehtml', 0);

        $t = array();
        $i = 0;

        foreach($ret->find('tr') as $h):
            $t$i]"title"] = $h->find('a', 1)->plaintext;

            $i++;
        endforeach;
        var_dump($t);

Et voici ce que me renvoie le var_dump():

array(40) { [0]=> array(1) { "title"]=> object(simple_html_dom_node)#636 (8) { "nodetype"]=> int(1)
 "tag"]=> string(1) "a" "attr"]=> array(1) { "href"]=> string(37) "fichier.html" }
 "children"]=> array(1) { [0]=> object(simple_html_dom_node)#638 (8) { "nodetype"]=> int(1) "tag"]=> string(1) "b" 
"attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(1) { [0]=> object(simple_html_dom_node)#639 (8) { 
"nodetype"]=> int(3) "tag"]=> string(4) "text" 
"attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(0) { } "parent"]=> object(simple_html_dom_node)#638 (8) 
{ "nodetype"]=> int(1) "tag"]=> string(1) "b" "attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(1) 
{ [0]=> object(simple_html_dom_node)#639 (8) { 
"nodetype"]=> int(3) "tag"]=> string(4) "text" "attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(0) { 
} "parent"]=> *RECURSION* "_"]=> array(1) { [4]=> string(5) "Harry" } "dom":"simple_html_dom_node":private]=> object(simple_html_dom)#4 
(18) { "root"]=> object(simple_html_dom_node)#5 (8) { "nodetype"]=> int(5) "tag"]=> string(4) "root" "attr"]=> array(0) { 
} "children"]=> array(2) { [0]=> object(simple_html_dom_node)#7 (8) { "nodetype"]=> int(6) "tag"]=> string(7) "unknown" 
"attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(0) { } "parent"]=> object(simple_html_dom_node)#5 (8) {
 "nodetype"]=> int(5) "tag"]=> string(4) "root" "attr"]=> array(0) { } "children"]=> array(2) { [0]=> object(simple_html_dom_node)#7 (8) {
 "nodetype"]=> int(6) "tag"]=> string(7) "unknown" "attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(0) { 
} "parent"]=> *RECURSION* "_"]=> array(2) { [0]=> int(2) [4]=> string(121) "" } "dom":"simple_html_dom_node":private]=> object(simple_html_dom)#4 (18) { 
"root"]=> *RECURSION* "nodes"]=> array(3431) { [0]=> *RECURSION* [1]=> object(simple_html_dom_node)#6 (8) { "nodetype"]=> int(3) 
"tag"]=> string(4) "text" "attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(0) { } "parent"]=> *RECURSION* 
"_"]=> array(1) { [4]=> string(1) " " }
 "dom":"simple_html_dom_node":private]=> *RECURSION* } [2]=> *RECURSION* [3]=> object(simple_html_dom_node)#8 (8) {
 "nodetype"]=> int(3) "tag"]=> string(4) "text" "attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(0) { 
} "parent"]=> *RECURSION* "_"]=> array(1) { [4]=> string(1) " " } "dom":"simple_html_dom_node":private]=> *RECURSION* } 
[4]=> object(simple_html_dom_node)#9 (8) { "nodetype"]=> int(1) "tag"]=> string(4) "html" "attr"]=> array(5) { 
"xmlns"]=> string(28) "http://www.w3.org/1999/xhtml" "xmlns:og"]=> string(17) "http://ogp.me/ns#" "xmlns:fb"]=> string(33)
 "http://www.facebook.com/2008/fbml" "xml:lang"]=> string(2) "fr" "lang"]=> string(2) "fr" } "children"]=> array(2) { 
[0]=> object(simple_html_dom_node)#11 (8) { "nodetype"]=> int(1) "tag"]=> string(4) "head" "attr"]=> array(0) { }
 "children"]=> array(39) { [0]=> object(simple_html_dom_node)#12 (8) { "nodetype"]=> int(1) "tag"]=> string(5) "title" 
"attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(1) { [0]=> object(simple_html_dom_node)#13 (8) { 
"nodetype"]=> int(3) "tag"]=> string(4) "text" "attr"]=> array(0) { } "children"]=> array(0) { } "nodes"]=> array(0) {
 } "parent"]=> object(simple_html_dom_node)#12 (8) { "nodetype"]=> int(1) "tag"]=> string(5) "title" "attr"]=> array(0) { 
} "children"]=> array(0) { } "nodes"]=> array(1) {
 [0]=> object(simple_html_dom_node)#13 (8) { "nodetype"]=> int(3) "tag"]=> string(4) "text" "attr"]=> array(0) { 
} "children"]=> array(0) { } "nodes"]=> array(0) { }
 "parent"]=> *RECURSION* "_"]=> array(1) { [4]=> string(37) " Films : Recherche" } 
"dom":"simple_html_dom_node":private]=> *RECURSION* } } "parent"]=> object(simple_html_dom_node)#11 (8) {
 "nodetype"]=> int(1) "tag"]=> string(4) "head" "attr"]=> array(0) { } "children"]=> array(39) { [0]=> *RECURSION* 
[1]=> object(simple_html_dom_node)#14 (8) { 
"nodetype"]=> int(1) "tag"]=> string(4) "meta" "attr"]=> array(2) { "http-equiv"]=> string(16) "Content-Language" 
"content"]=> string(2) "FR" } "children"]=> array(0) { }
 "nodes"]=> array(0) { } "parent"]=> *RECURSION* "_"]=> array(5) { [0]=> int(9) [2]=> array(2) { [0]=> int(0) [1]=> int(0) 
} [3]=> array(2) { [0]=> array(3) { [0]=> string(1) " " [1]=> string(0) "" 
[2]=> string(0) "" } [1]=> array(3) { [0]=> string(1) " " [1]=> string(0) "" [2]=> string(0) "" } }

Du moins une partie car le var_dump() fait planté mon navigateur.
Et puis plaintext me renvoie l'erreur suivante à chaque fois: Trying to get property of non-object in /Applications/MAMP/htdocs/html_dom/class/libs/parse.php on line 35

Avez-vous une idée de pourquoi ça fonctionne pas?

Car si à la place de $t"title"] = $h->find('a', 1)->plaintext je fais echo $h->find('a', 1)->plaintext et bien ça m'affiche bien mes titres.

Merci d'avance de votre aide.

1 réponse


Ne fais jamais de var_dump sur un objet de cette librairie ça ressort trop de chose et ça plante le navigateur. En fait l'erreur se traduirais par
Après pour ton erreur c'est super mystérieux... Il n'y a pas de raison qu'un echo marche et pas le $t...
Tu es sûr que tous les .purehtml ont un tr ?