2013-11-04 3 views
0

JSON을 구문 분석 할 HTML 표가 있습니다.PHP : HTML 표를 JSON으로 구문 분석

<table class="valas" border="0"> 
     <tr> 
      <th>Mata Uang</th><th>Jual</th><th>Beli</th> 
     </tr><tr class="odd"> 
      <td>USD</td><td class="number">11.450,00</td><td class="number">11.300,00</td> 
     </tr><tr class="even"> 
      <td>AUD</td><td class="number">11.094,00</td><td class="number">10.494,00</td> 
     </tr><tr class="odd"> 
      <td>CAD</td><td class="number">11.169,00</td><td class="number">10.669,00</td> 
     </tr><tr class="even"> 
      <td>CHF</td><td class="number">12.719,00</td><td class="number">12.219,00</td> 
     </tr><tr class="odd"> 
      <td>EUR</td><td class="number">15.678,00</td><td class="number">15.028,00</td> 
     </tr><tr class="even"> 
      <td>GBP</td><td class="number">18.525,00</td><td class="number">17.725,00</td> 
     </tr><tr class="odd"> 
      <td>HKD</td><td class="number">1.643,00</td><td class="number">1.293,00</td> 
     </tr><tr class="even"> 
      <td>JPY</td><td class="number">118,87</td><td class="number">113,37</td> 
     </tr><tr class="odd"> 
      <td>SAR</td><td class="number">3.233,00</td><td class="number">2.833,00</td> 
     </tr><tr class="even"> 
      <td>SGD</td><td class="number">9.454,00</td><td class="number">8.854,00</td> 
     </tr> 
    </table> 

그리고 제가 인터넷 검색에서 발견 된 일부 PHP 코드가 있습니다

{ 
"":{ 
    "":{ 
     "":true 
    } 
}, 
"USD":{ 
    "11.450,00":{ 
     "11.300,00":true 
    } 
}, 
"AUD":{ 
    "11.094,00":{ 
     "10.494,00":true 
    } 
}, 
"CAD":{ 
    "11.169,00":{ 
     "10.669,00":true 
    } 
}, 
"CHF":{ 
    "12.719,00":{ 
     "12.219,00":true 
    } 
}, 
"EUR":{ 
    "15.678,00":{ 
     "15.028,00":true 
    } 
}, 
"GBP":{ 
    "18.525,00":{ 
     "17.725,00":true 
    } 
}, 
"HKD":{ 
    "1.643,00":{ 
     "1.293,00":true 
    } 
}, 
"JPY":{ 
    "118,87":{ 
     "113,37":true 
    } 
}, 
"SAR":{ 
    "3.233,00":{ 
     "2.833,00":true 
    } 
}, 
"SGD":{ 
    "9.454,00":{ 
     "8.854,00":true 
    } 
} 
}null 

하지만 필요한 :

<?php 
include("simple_html_dom.php"); 
$html = file_get_html('index.html'); 
$row_count=0; 
$json = array(); 
foreach ($html->find('tr') as $row) { 
     $currency = $row->find('td',0)->innertext; 
     $sell = $row->find('td',1)->innertext; 
     $buy = $row->find('td',2)->innertext; 

     $json[$currency][$sell][$buy]=true; 
    } 
    echo json_encode($json); 
    ?> 

그리고 내가 얻은 것은 코드입니다, 그것은 잘못된 것 같습니다 is :

[{ 
"currency":"USD"{ 
    "sell":"11.450,00" 
    "buy":"11.300,00" 
}, 
"currency":"AUD"{ 
    "sell":"11.094,00" 
    "buy":"10.494,00" 
}, 
"currency":"CAD"{ 
    "sell":"11.169,00" 
     "buy":"10.669,00" 
}, 
"currency":"CHF"{ 
    "sell":"12.719,00" 
    "buy":"12.219,00" 
}, 
"currency":"EUR"{ 
    "sell":"15.678,00" 
    "buy":"15.028,00" 
}, 
"currency":"GBP"{ 
    "sell":"18.525,00" 
    "buy":"17.725,00" 
}, 
"currency":"HKD"{ 
    "sell":"1.643,00" 
    "buy":"1.293,00" 
}, 
"currency":"JPY"{ 
    "sell":"118,87" 
    "buy":"113,37" 
}, 
"currency":"SAR"{ 
    "sell":"3.233,00" 
    "buy":"2.833,00" 
}, 
"currency""SGD"{ 
    "sell":"9.454,00" 
    "boy":"8.854,00" 
} 
}] 

문제는 $ json 변수에 있지만 어려운 일입니다. 나는 이것이 당신이 원하는 무엇을 생각

답변

4

당신이을 필요 비록 유효 JSON하지 않습니다 이렇게하면 올바른 구조를 얻을 수 있습니다.

$json[] = [ 'currency' => $currency, 'sell' => $sell, 'buy' => $buy ]; 
+0

저에게 도움이됩니다. 감사. – SENDYYeah

+0

@SENDYYah 오신 것을 환영합니다. 그 때 대답을 수락하십시오! – moonwave99

0

.....

변경이 라인 :

$json[$currency][$sell][$buy]=true; 

에 :

$json[$row_count]['currency'] = $currency; 
$json[$row_count]['sell'] = $sell; 
$json[$row_count]['buy'] = $buy; 

$row_count++; 
0

여기에서 코드가 나타나면 : https://github.com/tremblay/HTML-Table-to-JSON 을 할 수 있습니다 당신이 이런 식으로 원하는 답변을 얻을 :

htmlToJSON ('index.html을', FALSE, NULL, NULL, 배열 (0 => '통화' , 1 => 'Buy', 2 => 'Sell'), null, null, TRUE);