开发者

PHP xml document: how to select and save

开发者 https://www.devze.com 2023-03-30 16:58 出处:网络
I have this html content, there are 500 tables like this in the page. $html=\" <table> <tbody>

I have this html content, there are 500 tables like this in the page.

$html="
<table>
   <tbody>
   <tr> 
     <td>
        <br>
        <strong>
           <font face='times new roman'>No.1 &nbsp &nbsp >AP00001 </font>
        </strong>
        <br> 
        <table width=90%>
            <tr></tr>
            <tr>
               <td><font color='#01A078'>    10.295</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     2.808</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     3.744</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.936</font></td>
               <td><font color='#01A078'>     5.615</font></td>
               <td><font color='#01A078'>     1.872</font></td>
            </tr>
            <tr>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     1.872</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     2.808</font></td>
               <td><font color='#01A078'>     0.936</font></td>
               <td><font color='#01A078'>     0.000</font></td>
            </tr>
            <tr>
               <td>    31.227</td>
               <td>    37.888</td>
            </tr>
        </table>
      </td>
    </tr>
    <tr> 
     <td> 
        <br>
        <strong>
           <font face='times new roman'>No.2 &nbsp &nbsp >AP00002 </font>
        </strong>
        <br>
        <table width=90%>
            <tr></tr>
            <tr>
               <td><font color='#01A078'>    10.295</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     2.808</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     3.744</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.936</font></td>
               <td><font color='#01A078'>     5.615</font></td>
               <td><font color='#01A078'>     1.872</font></td>
            </tr>
            <tr>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078开发者_运维知识库'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     1.872</font></td>
               <td><font color='#01A078'>     0.000</font></td>
               <td><font color='#01A078'>     2.808</font></td>
               <td><font color='#01A078'>     0.936</font></td>
               <td><font color='#01A078'>     0.000</font></td>
            </tr>
            <tr>
               <td>    35.267</td>
               <td>    17.834</td>
            </tr>
        </table>
      </td>
    </tr>
     .
     .
     .
     .
   </tbody>
 </table>";

as you see the third <tr> is different , and I want to save that two number in variables ( 31.227 and 37.888) related with the value inside the <strong> element.

my desired output looks like this :

No.1 >AP00001 31.227 37.888
No.2 >AP00002 35.267 17.834
.
.
.

any help? how can I do that in PHP?


$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$keys = array();
$names = $xpath->query('//table/tbody/tr/td/strong/font');
foreach ($names as $name){
    $keys []= trim($name->nodeValue);
}
$values = array();
$trs = $xpath->query('//table/tbody/tr/td/table/tr[4]');
foreach($trs as $tr) {
    $tds = $tr->getElementsByTagName('td');
    $values []= array(trim($tds->item(0)->nodeValue), trim($tds->item(1)->nodeValue));
}
$result = array_combine($keys, $values);
echo '<pre>' . print_r($result, true) . '</pre>';

Not the most elegant, but'll do the job:

Array
(
    [No.1     >AP00001] => Array
        (
            [0] => 31.227
            [1] => 37.888
        )

    [No.2     >AP00002] => Array
        (
            [0] => 35.267
            [1] => 17.834
        )

)
0

精彩评论

暂无评论...
验证码 换一张
取 消