PHP 根据 HTML 表解析文件中的另一个值查找和获取值

PHP find and get value based on another one from HTML table parsed file

本文关键字:另一个 查找 获取 文件 HTML 根据 PHP      更新时间:2023-09-26

我正在为我的项目使用PHP Simple HTML DOM解析器。

我正在尝试查找特定数据并在从包含 HTML 表元素中的数据的 URL 网站解析我的 .php 文件后回显它,示例如下:

<table class="example">
 <tbody>
  <tr>
   <td>
     Heading #1
     <p>Description of heading #1 here ...</p>
   </td>
   <td>Example of data #1</td>
  </tr>
  <tr>
   <td>
     Heading #2
     <p>Description of heading #2 here ...</p>
   </td>
   <td>Example of data #2</td>
  </tr>
 </tbody>
</table>

我的问题:

如何通过知道同一 TR 行中的第一个 TD 单元格包含值"标题 #1..."从这种桌子?

我已经解析了URL,现在我需要根据它旁边的另一个值找到值。

我应该使用一些正则表达式并为此制作一些模式吗? strpos() 和数组?

你需要给表分区一个 JavaScript 的 ID,以便能够获取要提交的数据,并将其放入带有名称和 ID 的隐藏输入中,以便 PHP 使用 POST 获取它们。

<script language="javascript">
function transfer_data(){
documentGetElementById('ex1_hidden').value = documentGetElementById('ex1').innerHTML;
documentGetElementById('ex2_hidden').value = documentGetElementById('ex2').innerHTML;
submit();
} 
</script>
       <table class="example">
         <tbody>
          <tr>
           <td id="hdg1">
             Heading #1
             <p>Description of heading #1 here ...</p>
           </td>
           <td id="ex1">Example of data #1</td>
          </tr>
          <tr>
           <td>
             Heading #2
             <p>Description of heading #2 here ...</p>
           </td>
           <td id="ex2">Example of data #2</td>
          </tr>
         </tbody>
        </table>

在您的表格中,使用您需要的method="post"提交到您希望它去的任何地方:

    <input type="hidden" name="ex1_hidden" id="ex1_hidden" />
    <input type="hidden" name="ex2_hidden" id="ex2_hidden" />

    <input type="button" value="Submit" onClick="transfer_data()" />

在PHP中,你会用$_POST['ex1_hidden']$_POST['ex2_hidden']来拿起它们(记得清理提交的数据)。

这不是适合安全数据的方法。

您可以向标题添加 ID,并在脚本中使其成为条件:

if(documentGetElementById('hdg1').innerHTML == "Heading #1"){
   documentGetElementById('ex1_hidden').value = documentGetElementById('ex1').innerHTML;
}

您可能需要使用类似的东西修剪标题上的空格

    var str=documentGetElementById('hdg1').innerHTML.replace(/^'s+|'s+$/g,'');

关于使用 jQuery 抓取文本时如何去除空格的信用@Paul?

这里有很多关于其他方式的有用想法 如何使用jQuery获取表单元格值?

如果这是从另一个网站抓取的数据,你根本无法控制,但你已经在PHP变量中拥有,你可以通过<td>explode()它,并计算出哪些数组位置包含你想要的数据。参考: http://php.net/manual/en/function.explode.php

这就是我认为您真正正在寻找的 - 首先询问网站所有者是否可以使用可能是个好主意,但这取决于您。您在使用strpos();和数组(使用您的表进行测试)方面走在正确的轨道上:

 // only works if fopen is allowed on the site's server and in PHP5+
 $handle = fopen("http://websiteyouwanttoscrape.com/file.html", "r"); 
 $contents = stream_get_contents($handle);
 $contents_array = array();
 $bit_i_want = array();
 // give yourself a chance
 $contents = htmlspecialchars($contents);
 // swap these if you don't use htmlspecialchars();
 $contents_array = explode('&lt;td&gt;',$contents);
 //$contents_array = explode('<td>',$contents);
 $counter = 0;
 while($counter < count($contents_array)){
      if(strpos($contents_array[$counter], 'Heading #1') > 0 ){
          // swap these if you don't use htmlspecialchars();
          $bit_i_want = explode('&lt;/td&gt;',$contents_array[$counter+1]);
          //$bit_i_want = explode('</td>',$contents_array[$counter+1]);
          echo $bit_i_want[0] . '<br />';
          // uncomment break; to stop the loop if you don't
          // want to look for any more instances of "Heading #1" if there were any
          //break;
      }
 $counter++;
 }
 fclose($handle); //close the file