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


i using php simple html dom parser project.

i trying find specific data , echo after parse .php file url website contains data inside html table element, example 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 value "example of data #1" second td cell element in first tr row element knowing first td cell in same tr row contains value "heading #1 ..." kind of table?

i have parsed url, need find value based on other value next it.

should use regex , make pattern that? strpos() , array?

you need give table divisions id javascript able data submission , put hidden inputs names , ids php 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 form submits wherever want go using method="post" 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 pick them $_post['ex1_hidden'] , $_post['ex2_hidden'] (remember clean submitted data.)

this not method suitable for secure data.

you add id heading , make conditional in script:

if(documentgetelementbyid('hdg1').innerhtml == "heading #1"){    documentgetelementbyid('ex1_hidden').value = documentgetelementbyid('ex1').innerhtml; } 

you might need trim whitespace off heading perhaps using like

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

credit @paul on how strip white space when grabbing text jquery?

lots of useful ideas on other ways here how table cell value using jquery?

if scraped data website don't have control on @ all, have in php variable, explode() <td> , work out array positions contain data want. ref: http://php.net/manual/en/function.explode.php

this think looking - might nice idea ask owner of site if ok first you. on right track strpos(); , arrays (tested using table):

 // works if fopen allowed on site's server , in php5+  $handle = fopen("http://websiteyouwanttoscrape.com/file.html", "r");    $contents = stream_get_contents($handle);  $contents_array = array();  $bit_i_want = array();   // give chance  $contents = htmlspecialchars($contents);   // swap these if 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 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; stop loop if don't           // want more instances of "heading #1" if there           //break;       }  $counter++;  }  fclose($handle); //close file 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -