Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...