2011-11-27 12 views
12

hereの表を多次元PHP配列に解析しようとしています。私は次のコードを使用していますが、何らかの理由で空の配列を返しています。 Web上で検索した後、私はthis siteを見つけました。そこにはparseTable()関数があります。そのウェブサイトのコメントを読んでから、私はその機能が完璧に機能していることがわかります。ですから、私はfile_get_contents()からHTMLコードを取得する方法に何か問題があると仮定しています。私が間違ってやっていることに関する考えは?phtml配列にfile_get_contentsを使用してhtmlテーブルを解析する

<?php 

$data = file_get_contents('http://flow935.com/playlist/flowhis.HTM'); 

function parseTable($html) 
{ 
    // Find the table 
    preg_match("/<table.*?>.*?<\/[\s]*table>/s", $html, $table_html); 

    // Get title for each row 
    preg_match_all("/<th.*?>(.*?)<\/[\s]*th>/", $table_html[0], $matches); 
    $row_headers = $matches[1]; 

    // Iterate each row 
    preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches); 

    $table = array(); 

    foreach($matches[1] as $row_html) 
    { 
    preg_match_all("/<td.*?>(.*?)<\/[\s]*td>/", $row_html, $td_matches); 
    $row = array(); 
    for($i=0; $i<count($td_matches[1]); $i++) 
    { 
     $td = strip_tags(html_entity_decode($td_matches[1][$i])); 
     $row[$row_headers[$i]] = $td; 
    } 

    if(count($row) > 0) 
     $table[] = $row; 
    } 
    return $table; 
} 

$output = parseTable($data); 

print_r($output); 

?> 

私は私の出力配列は、このような何かを見てみたい:

 
1 
--> 11:33AM 
--> DEV 
--> IN THE DARK 

2 
--> 11:29AM 
--> LIL' WAYNE 
--> SHE WILL 

3 
--> 11:26AM 
--> KARDINAL OFFISHALL 
--> NUMBA 1 (TIDE IS HIGH) 
+1

-1。代わりに、基本的にコードの巨大なブロックを掲示するのあなたの問題を特定し、人々にイチジクを求める何が間違っているかを確認して修正してください。 – NullUserException

答えて

43

は、正規表現でHTMLを解析し、自分自身を不自由しないでください!代わりに、HTMLパーサーライブラリにマークアップの構造について心配する必要があります。

シンプルなHTML DOM(http://simplehtmldom.sourceforge.net/)をチェックすることをお勧めします。これは、PHPのこの種のWebスクラップング問題を解決するために特別に書かれたライブラリです。このようなライブラリを使うことで、正規表現の作成を心配することなく、ずっと少ないコード行で擦り傷を書くことができます。これは、その後、などのアーティストと対応するタイトルの配列を作成するインスタンスのために、いくつかの形式でデータをキャプチャするように拡張することができ

$html = file_get_html('http://flow935.com/playlist/flowhis.HTM'); 
foreach($html->find('tr') as $row) { 
    // Parse table row here 
} 

:シンプルなHTML DOMであなただけのような何かを書く原則

、:

<?php 
require('simple_html_dom.php'); 

$table = array(); 

$html = file_get_html('http://flow935.com/playlist/flowhis.HTM'); 
foreach($html->find('tr') as $row) { 
    $time = $row->find('td',0)->plaintext; 
    $artist = $row->find('td',1)->plaintext; 
    $title = $row->find('td',2)->plaintext; 

    $table[$artist][$title] = true; 
} 

echo '<pre>'; 
print_r($table); 
echo '</pre>'; 

?> 

私たちは、このコードは(自明)ことができることを見ることができるだけでなく、他の方法でデータを再フォーマットするように変更しました。

+0

それは完全に機能しました。しかし、元の質問の一番下に示されているように、多次元配列を作成する必要があります。 –

+0

"Slrapdotをこする"という例をチェックしましたか? simplehtmldomサイトから?私が理解する限り、それはその質問に答える。 – jsalonen

+1

私はもう一つの例を追加しましたが、これは私が行く限りです。私はあなたが理解するために残りを残します。 – jsalonen

17

私はsimple_html_domを試しましたが、大きなファイルと関数の繰り返し呼び出しで、PHP 5.3(GAH)でzend_mm_heap_corruptedになっています。私はまた、preg_match_allを試してみました(これは私のHTMLテーブルの約400行だった大きなファイル(5000)HTMLの行、に失敗してきた。私はこれを使用していますし、その高速な作業やエラーを吐きない

$dom = new DOMDocument(); 

//load the html 
$html = $dom->loadHTMLFile("htmltable.html"); 

    //discard white space 
$dom->preserveWhiteSpace = false; 

    //the table by its tag name 
$tables = $dom->getElementsByTagName('table'); 


    //get all rows from the table 
$rows = $tables->item(0)->getElementsByTagName('tr'); 
    // get each column by tag name 
$cols = $rows->item(0)->getElementsByTagName('th'); 
$row_headers = NULL; 
foreach ($cols as $node) { 
    //print $node->nodeValue."\n"; 
    $row_headers[] = $node->nodeValue; 
} 

$table = array(); 
    //get all rows from the table 
$rows = $tables->item(0)->getElementsByTagName('tr'); 
foreach ($rows as $row) 
{ 
    // get each column by tag name 
    $cols = $row->getElementsByTagName('td'); 
    $row = array(); 
    $i=0; 
    foreach ($cols as $node) { 
     # code... 
     //print $node->nodeValue."\n"; 
     if($row_headers==NULL) 
      $row[] = $node->nodeValue; 
     else 
      $row[$row_headers[$i]] = $node->nodeValue; 
     $i++; 
    } 
    $table[] = $row; 
} 

var_dump($table); 

このコードは私のためによく働いた。元のコードの 例はこちらです。努力の不足のため

http://techgossipz.blogspot.co.nz/2010/02/how-to-parse-html-using-dom-with-php.html

+1

はい私のために働いて! :) :) –

+0

最初の要素は[0] =>配列(0){ } になるので、array_shift($ table)を実行する必要があります。これは、すべてのtrタグを$タグのタグを含む$行に含めているからです。私は編集を提案する。 –

+0

私はPHP 5.6.31でDOMを使用していますが、 '$ rows = $ tables-> item(0) - > getElementsByTagName( 'tr')'の結果に '​​' $ cols = $ row-> getElementsByTagName( 'td') 'です。 'getElementsByTagName()'への私の最初の呼び出しがHTMLタグを取り除いているように見える理由は何ですか? – Tony