<bdo id='AMgEU'></bdo><ul id='AMgEU'></ul>
  • <tfoot id='AMgEU'></tfoot>
    1. <legend id='AMgEU'><style id='AMgEU'><dir id='AMgEU'><q id='AMgEU'></q></dir></style></legend>

      <small id='AMgEU'></small><noframes id='AMgEU'>

      1. <i id='AMgEU'><tr id='AMgEU'><dt id='AMgEU'><q id='AMgEU'><span id='AMgEU'><b id='AMgEU'><form id='AMgEU'><ins id='AMgEU'></ins><ul id='AMgEU'></ul><sub id='AMgEU'></sub></form><legend id='AMgEU'></legend><bdo id='AMgEU'><pre id='AMgEU'><center id='AMgEU'></center></pre></bdo></b><th id='AMgEU'></th></span></q></dt></tr></i><div id='AMgEU'><tfoot id='AMgEU'></tfoot><dl id='AMgEU'><fieldset id='AMgEU'></fieldset></dl></div>

        仅从 php 中使用的 preg_match_all 的 html 表中获取数

        时间:2023-10-04

            <tbody id='kmpqy'></tbody>
              <bdo id='kmpqy'></bdo><ul id='kmpqy'></ul>

            • <i id='kmpqy'><tr id='kmpqy'><dt id='kmpqy'><q id='kmpqy'><span id='kmpqy'><b id='kmpqy'><form id='kmpqy'><ins id='kmpqy'></ins><ul id='kmpqy'></ul><sub id='kmpqy'></sub></form><legend id='kmpqy'></legend><bdo id='kmpqy'><pre id='kmpqy'><center id='kmpqy'></center></pre></bdo></b><th id='kmpqy'></th></span></q></dt></tr></i><div id='kmpqy'><tfoot id='kmpqy'></tfoot><dl id='kmpqy'><fieldset id='kmpqy'></fieldset></dl></div>

              <small id='kmpqy'></small><noframes id='kmpqy'>

                1. <tfoot id='kmpqy'></tfoot>
                2. <legend id='kmpqy'><style id='kmpqy'><dir id='kmpqy'><q id='kmpqy'></q></dir></style></legend>
                  本文介绍了仅从 php 中使用的 preg_match_all 的 html 表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个这样的 html 表格:

                  I have a html table like this :

                  <table ... >
                  
                    <tbody ... >
                  
                         <tr ... > 
                               <td ...>
                                    string...
                                </td>
                                  <td ...>
                                    string...
                                </td>
                                  <td ...>
                                    string...
                                </td>
                                  <td ...>
                                    string...
                                </td>
                                  <td ...>
                                    string...
                                </td>
                         </tr>
                          <tr ... > 
                               <td ...>
                                    string...
                                </td>
                                  <td ...>
                                    string...
                                </td>
                                  <td ...>
                                    string...
                                </td>
                                  <td ...>
                               </td>
                                  <td ...>
                                    string...
                                </td>
                         </tr>
                         ..............
                  
                    </tbody>
                  
                  
                  </table>
                  

                  这是一个数据表,我需要从中获取所有数据.该表将有许多行 ().每行都有一个固定的列()(目前是 5 ).记住每个表、tr、td 标签可能已格式化(其中说...")

                  This is a data table and I need to get all data from this. The table will have many rows (<tr></tr>) . each row will have a fixed columns (<td></td>)(currently is 5 ). remember each table,tr,td tag maybe formatted (where say "...")

                  我希望大家能帮我写一个正则表达式用于 preg_match_all 函数来获取这样的数据:

                  And I hope everyone can help me to write a regex for preg_match_all function to get the data like this :

                  array(
                     0 => array(
                         0=> 'some data0',
                         1=> 'some data1',
                         2=> 'some data2',
                         3=> 'some data3',
                         4=> 'some data4',
                     )
                     1 => array(
                         0=> 'some data0',
                         1=> 'some data1',
                         2=> 'some data2',
                         3=> 'some data3',
                         4=> 'some data4',
                     )
                     2 => array(
                         0=> 'some data0',
                         1=> 'some data1',
                         2=> 'some data2',
                         3=> 'some data3',
                         4=> 'some data4',
                     )
                  ..........
                  )
                  

                  现在是你的测试示例,希望你能帮助我!!!

                  Now the example for your test, hopfully you can help me!!!

                  <table border="1" >
                    <tbody style="" >
                  
                         <tr style="" > 
                               <td style="color:blue;">
                                    data0
                                </td>
                                  <td style="font-size:15px;">
                                   data1
                                </td>
                                  <td style="font-size:15px;">
                                    data2
                                </td>
                                  <td style="color:blue;">
                                    data3
                                </td>
                                  <td style="color:blue;">
                                    data4
                                </td>
                         </tr>
                         <tr style="" > 
                               <td style="color:blue;">
                                    data00
                                </td>
                                  <td style="font-size:15px;">
                                   data11
                                </td>
                                  <td style="font-size:15px;">
                                    data22
                                </td>
                                  <td style="color:blue;">
                                    data33
                                </td>
                                  <td style="color:blue;">
                                    data44
                                </td>
                         </tr>
                         <tr style="color:black" > 
                               <td style="color:blue;">
                                    data000
                                </td>
                                  <td style="font-size:15px;">
                                   data111
                                </td>
                                  <td style="font-size:15px;">
                                    data222
                                </td>
                                  <td style="color:blue;">
                                    data333
                                </td>
                                  <td style="color:blue;">
                                    data444
                                </td>
                         </tr>
                  
                    </tbody>
                  
                  
                  </table>
                  

                  推荐答案

                  你绝对不想用 Regex 解析 HTML.

                  You absolutely do NOT want to parse HTML with Regex.

                  有太多的变体,一方面,更重要的是,正则表达式对于 HTML 的层次结构不是很好.最好使用 XML 解析器或更好的 HTML 特定解析器.

                  There are far too many variations, for one, and more importantly, regex isn't very good with the hierarchal nature of HTML. It's best to use an XML parser or better-yet an HTML-specific parser.

                  每当我需要抓取 HTML 时,我倾向于使用 Simple HTML DOM Parser 库,它需要一个HTML 树并将其解析为可遍历的 PHP 对象,您可以在该对象中查询类似 JQuery 的内容.

                  Whenever I need to scrape HTML, I tend to use the Simple HTML DOM Parser library, which takes an HTML tree and parses it into a traversable PHP object, which you can query something like JQuery.

                  <?php
                      require 'simplehtmldom/simple_html_dom.php';
                  
                      $sHtml = <<<EOS
                      <table border="1" >
                        <tbody style="" >
                             <tr style="" > 
                                   <td style="color:blue;">
                                        data0
                                    </td>
                                      <td style="font-size:15px;">
                                       data1
                                    </td>
                                      <td style="font-size:15px;">
                                        data2
                                    </td>
                                      <td style="color:blue;">
                                        data3
                                    </td>
                                      <td style="color:blue;">
                                        data4
                                    </td>
                             </tr>
                             <tr style="" > 
                                   <td style="color:blue;">
                                        data00
                                    </td>
                                      <td style="font-size:15px;">
                                       data11
                                    </td>
                                      <td style="font-size:15px;">
                                        data22
                                    </td>
                                      <td style="color:blue;">
                                        data33
                                    </td>
                                      <td style="color:blue;">
                                        data44
                                    </td>
                             </tr>
                             <tr style="color:black" > 
                                   <td style="color:blue;">
                                        data000
                                    </td>
                                      <td style="font-size:15px;">
                                       data111
                                    </td>
                                      <td style="font-size:15px;">
                                        data222
                                    </td>
                                      <td style="color:blue;">
                                        data333
                                    </td>
                                      <td style="color:blue;">
                                        data444
                                    </td>
                             </tr>
                        </tbody>
                      </table>
                  EOS;
                  
                      $oHTML = str_get_html($sHtml);
                      $oTRs = $oHTML->find('table tr');
                      $aData = array();
                      foreach($oTRs as $oTR) {
                          $aRow = array();
                          $oTDs = $oTR->find('td');
                  
                          foreach($oTDs as $oTD) {
                              $aRow[] = trim($oTD->plaintext);
                          }
                  
                          $aData[] = $aRow;
                      }
                  
                      var_dump($aData);
                  ?>
                  

                  和输出:

                  array
                    0 => 
                      array
                        0 => string 'data0' (length=5)
                        1 => string 'data1' (length=5)
                        2 => string 'data2' (length=5)
                        3 => string 'data3' (length=5)
                        4 => string 'data4' (length=5)
                    1 => 
                      array
                        0 => string 'data00' (length=6)
                        1 => string 'data11' (length=6)
                        2 => string 'data22' (length=6)
                        3 => string 'data33' (length=6)
                        4 => string 'data44' (length=6)
                    2 => 
                      array
                        0 => string 'data000' (length=7)
                        1 => string 'data111' (length=7)
                        2 => string 'data222' (length=7)
                        3 => string 'data333' (length=7)
                        4 => string 'data444' (length=7)
                  

                  这篇关于仅从 php 中使用的 preg_match_all 的 html 表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在html表格中显示一个数组 下一篇:从文本文件中获取数据并将其显示在 html 表中

                  相关文章

                  最新文章

                3. <tfoot id='ZaWrL'></tfoot>
                4. <legend id='ZaWrL'><style id='ZaWrL'><dir id='ZaWrL'><q id='ZaWrL'></q></dir></style></legend>
                5. <i id='ZaWrL'><tr id='ZaWrL'><dt id='ZaWrL'><q id='ZaWrL'><span id='ZaWrL'><b id='ZaWrL'><form id='ZaWrL'><ins id='ZaWrL'></ins><ul id='ZaWrL'></ul><sub id='ZaWrL'></sub></form><legend id='ZaWrL'></legend><bdo id='ZaWrL'><pre id='ZaWrL'><center id='ZaWrL'></center></pre></bdo></b><th id='ZaWrL'></th></span></q></dt></tr></i><div id='ZaWrL'><tfoot id='ZaWrL'></tfoot><dl id='ZaWrL'><fieldset id='ZaWrL'></fieldset></dl></div>
                    <bdo id='ZaWrL'></bdo><ul id='ZaWrL'></ul>

                    1. <small id='ZaWrL'></small><noframes id='ZaWrL'>