如何使用preg_match_all解析html与PHP

时间:2017-05-16 21:38:34

标签: php html

嘿,我试图用PHP解析html。我的php脚本如下

    <?php

   $page = file_get_contents('http://localhost/pim/station.php');
   preg_match_all('/<td.*?>(.*?)<\/td>/si', $page, $agent_name);
    echo json_encode($agent_name);
      ?>

这是我试图解析的HTML

          <form name="maforme" method="post" action="">
            <center class="p2Text">

             <div id='table'>
               <table id="tablex" width="99%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#111111" style="font-family: Arial; font-size: 10px; border-collapse: collapse; height: 53px" name="table">
               <thead>
                 <tr align="center"  >
                   <th height="30" colspan="7" align="center" valign="middle" bgcolor="#F2FAE7" class="sous_TITRE" >
                     TUNIS                         &nbsp;=====>&nbsp;
                     SIDI AHMED                         </th>
  </tr>
                 <tr align="center">
                   <th width="110" height="24" colspan="1" valign="middle" bgcolor="#F2FAE7" class="titre_tableau">Ligne</th>
  <th width="72" height="24" colspan="1" valign="middle" bgcolor="#F2FAE7" class="titre_tableau">Station d&eacute;part</th>
  <th width="50" height="24" valign="middle" bgcolor="#F2FAE7" class="titre_tableau">Heure d&eacute;part </th>
  <th width="68" height="24" colspan="1" valign="middle" bgcolor="#F2FAE7" class="titre_tableau">Station arriv&eacute;e</th>
  <th width="52" height="24" valign="middle" bgcolor="#F2FAE7" class="titre_tableau">Heure arriv&eacute;e</th>
  <th width="51" height="24" valign="middle" bgcolor="#F2FAE7" class="titre_tableau">Distance</th>
  <th width="47" valign="middle" bgcolor="#F2FAE7" class="titre_tableau">Tarif (DT) </th>
  </tr>
                 </thead>
               <tbody id="offTblBdy">




                 <tr class="tabnormal" onMouseOver="this.className='tabover'" onMouseOut="this.className='tabnormal'">
                   <td height="30" align="left" valign="middle" class="text_normal_colore">TUNIS - TABARKA <span class="unnamed1">(N)</span></td>
                      <td height="30" align="center" valign="middle" class="bluetext" >&nbsp; TUNIS                            <input type="hidden" value="7"> </td>
                      <td height="30" align="center" valign="middle"  >&nbsp; 06:30 </td>
                      <td height="30" align="center" valign="middle" class="bluetext">&nbsp; SIDI AHMED </td>
                      <td height="30" align="center" valign="middle" >&nbsp;07:40 </td>

                            <td height="30" align="center" valign="middle" class="style10">&nbsp;70 </td>
                         <td height="30" align="center" valign="middle" class="text_normal_colore">4.190</td>
                 </tr>

实际上我正在尝试用id offTblBdy 来解析表格的td内的值.PLZ可以有人帮忙

0 个答案:

没有答案