Je reformule ma discussion en essayant d'être plus clair :

J'utilise simple_html_dom.php

Je veux retirer le premier enfant d'un élément div.


Le code HTML :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
<div id="result">
    <a class="result_type1" href="#">The title</a>
    <span class="item">item</span>
    <span class="more">more</span> 
    <span class="description">description</span>
    </div>
Le code PHP essai 1 :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
foreach($html2->find("div[id=result]") as $element) 
     {
    if ($element->find('.result_type1',0)->plaintext!='') 
    {
 
    // retire result_type1
 
    $element->children(0)->outertext=$element->children(1)->outertext;
    $element->children(1)->outertext=$element->children(2)->outertext;
    $element->children(2)->outertext=$element->children(3)->outertext;
    $element->children(3)->outertext="";
 
    echo $element->children(0)->plaintext; 
 
    }
Résultat (incohérent) :


Code PHP essai 2 :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   foreach($html2->find("div[id=result]") as $element) 
     {
    if ($element->find('.result_type1',0)->plaintext!='') 
    {
 
    // retire result_type1
 
    $element->children(0)->outertext=$element->children(1)->outertext;
    $element->children(1)->outertext=$element->children(2)->outertext;
    $element->children(2)->outertext=$element->children(3)->outertext;
    $element->children(3)->outertext="";
 
    echo $element->children(0)->outertext; 
 
    }
Résultat (bon):

Code : Sélectionner tout - Visualiser dans une fenêtre à part
<span class="item">item</span>

Je n'obtient pas le même élément.
plaintext affiche l'élément qui est censé avoir été supprimé.