2010-07-18 13 views
6

ile bir tablonun hücreleri nasıl yazdırılır bu html kodu var. Verileri kendi php betiğimde ayrıştırmak için Simple HTML Dom kullanıyorum.basit html dom

<table> 
    <tr> 
     <td class="header">Name</td> 
     <td class="header">City</td> 
    </tr> 
    <tr> 
     <td class="text">Greg House</td> 
     <td class="text">Century City</td> 
    </tr> 
    <tr> 
     <td class="text">Dexter Morgan</td> 
     <td class="text">Miami</td> 
    </tr> 
</table> 

Ben örnek, bir dizideki TD'leri içindeki metni almak gerekir:

$ dizi [0] = array ('Greg Evi', 'Century City'); $ dizi [1] = dizi ('Dexter Morgan', 'Miami');

Bunu elde etmenin birkaç yolunu denedim ama her birinde ve hepsinde başarısız oldum. Birisi bana yardım edebilir mi?

+0

[DOM ve Xpath] (http://stackoverflow.com/questions/2019422/regex-problem-in-php) – quantumSoup

+0

kullanmalısınız. Basit HTML Dom kullanarak yapmak için;) – andufo

cevap

11

Bu yapmalıdır:

// get the table. Maybe there's just one, in which case just 'table' will do 
$table = $html->find('#theTable'); 

// initialize empty array to store the data array from each row 
$theData = array(); 

// loop over rows 
foreach($table->find('tr') as $row) { 

    // initialize array to store the cell data from each row 
    $rowData = array(); 
    foreach($row->find('td.text') as $cell) { 

     // push the cell's text to the array 
     $rowData[] = $cell->innertext; 
    } 

    // push the row's data array to the 'big' array 
    $theData[] = $rowData; 
} 
print_r($theData); 
+0

mükemmel çalıştı. Teşekkürler! – andufo

+0

@ andufo, @ Karim, Bana dom nesnede nasıl nesne ($ html) yaptığınızı söyler misiniz? Yani bunu şöyle kullandınız: - $ table = $ html-> find ('# theTable'); – Bajrang

1

@lucia nie

Çok yapmalısınız:

// initialize empty array to store the data array from each row 
$theData = array(); 

// loop over rows 
foreach($html->find('#theTable tr') as $row) { 

// initialize array to store the cell data from each row 
$rowData = array(); 
foreach($row->find('td.text') as $cell) { 

    // push the cell's text to the array 
    $rowData[] = $cell->innertext; 
} 

// push the row's data array to the 'big' array 
$theData[] = $rowData; 
} 
print_r($theData); 
3

İşe yarayacak .. bu

include('simple_html_dom.php'); 
$html = file_get_html('mytable.html'); 
foreach($html->find('table tr td') as $e){ 
    $arr[] = trim($e->innertext); 
    } 

print_r($arr); 
deneyin

Herhangi bir html etiketinden bile veri alabilirsin ...

İlgili konular