PHP根据HTML表格解析文件中的另一个查找并获取值 [英] PHP find and get value based on another one from HTML table parsed file

查看:157
本文介绍了PHP根据HTML表格解析文件中的另一个查找并获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

 < table class =example> 
< tbody>
< tr>
< td>
标题#1
< p>标题#1的描述在这里...< / p>
< / td>
< td>数据#1的示例< / td>
< / tr>
< tr>
< td>
标题#2
< p>标题#2的描述在这里...< / p>
< / td>
< td>数据#2的示例< / td>
< / tr>
< / tbody>
< / table>

我的问题:

通过知道同一TR行中的第一TD单元包含来自这种表的值标题#1 ...的值,从第一TR行元素中的第二TD单元元素获得值数据#1的示例 / p>

我已经解析了网址,现在我需要根据旁边的其他值查找值。



我应该使用一些正则表达式并为此创建一些模式? strpos()和array?

解决方案

您需要为表格部门提供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>
标题#1
< p>标题#1的描述在这里...< / p>
< / td>
< td id =ex1>数据#1的示例< / td>
< / tr>
< tr>
< td>
标题#2
< p>标题#2的描述在这里...< / p>
< / td>
< td id =ex2>数据#2的示例< / td>
< / tr>
< / tbody>
< / table>

在您提交给您希望使用 method = post您需要:

 < input type =hiddenname =ex1_hidden id =ex1_hidden/> 
< input type =hiddenname =ex2_hiddenid =ex2_hidden/>


< input type =buttonvalue =SubmitonClick =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, ''); 

致谢@Paul on



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



如果这是从其他网站上截取的数据,而您根本无法控制它,但是您已经在一个PHP变量中,可以通过< td> explode()>来确定哪些数组位置包含你想要的数据。参考: http://php.net/manual/en/function.explode.php

这就是我认为你真的在寻找的东西 - 可能是一个好主意,问问网站的所有者是否可以,但是这已经到了给你。你在 strpos(); 和数组(使用你的表进行测试)的正确轨道上:

  //只有在网站的服务器和PHP5中允许使用fopen时才可以使用
$ handle = fopen(http://websiteyouwanttoscrape.com/file.html,r);

$ contents = stream_get_contents($ handle);
$ contents_array = array();
$ bit_i_want = array();

//给自己一个机会
$ contents = htmlspecialchars($ contents);

//如果你不使用htmlspecialchars();
$ contents_array =爆炸('< td& gt;',$ contents);
// $ contents_array = explode('< td>',$ contents);

$ counter = 0;
while($ counter< count($ contents_array)){
if(strpos($ contents_array [$ counter],'Heading#1')> 0){
// swap这些如果你不使用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 />';
//取消注释;如果你没有
//要停止循环,如果有任何
// break,就要寻找更多的Heading#1的实例;
}
$ counter ++;
}
fclose($ handle); //关闭文件


I am using PHP Simple HTML DOM Parser for my project.

I am trying to find specific data and echo it after I parse my .php file from a URL Website which contains data inside HTML table element, example is below:

<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>

My question:

How can I get value "Example of data #1" from the second TD cell element in first TR row element by knowing that the first TD cell in the same TR row contains value "Heading #1 ..." from this kind of a table?

I have parsed URL, now I need to find value based on the other value which is next to it.

Should I use some regex and make some pattern for that? strpos() and array?

解决方案

You would need to give the table divisions an ID for JavaScript to be able to get the data for submission and put it into hidden inputs with names and IDs so that PHP will get them using 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>

In your form which submits to wherever you want it to go using method="post" you would need:

    <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()" />

In PHP you would pick them up with $_POST['ex1_hidden'] and $_POST['ex2_hidden'] (remember to clean up submitted data.)

This is not a method which would be suitable for for secure data.

You could add an ID to the heading and make it conditional in your script:

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

You might need to trim the whitespace off the heading perhaps by using something like

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

Credit @Paul on how do I strip white space when grabbing text with jQuery?

Lots of useful ideas on other ways here How to get a table cell value using jQuery?

If this is scraped data from another website which you don't have control over at all, but which you already have in a PHP variable, you could explode() it by <td> and work out which array positions contain the data you want. Ref: http://php.net/manual/en/function.explode.php

This is what I think you are really looking for - might be a nice idea to ask the owner of the site if it is OK first but that is up to you. You were on the right track with strpos(); and arrays (tested using your table):

 // 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

这篇关于PHP根据HTML表格解析文件中的另一个查找并获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆