-1
<table border='0' cellspacing='3' width='100%'>
   <tr>
      <td width='15%'><font color='#3366FF'><b>Trading Code:</b></font></td>
      <td width='85%'>RCL</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News Title:</b></font></td>
      <td>DSENEWS: Withdrawal of Authorized Representative</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News:</b></font></td>
      <td align='justify'>Withdrawal of Authorized Representative: Royal Capital Ltd., DSE TREC No. 21, has withdrawn one of its Authorized Representatives, Mr. Md. Zikrul Haque, with immediate effect.</td>
   </tr>
</table>
<br>
<table border='0' cellspacing='3' width='100%'>
   <tr>
      <td width='15%'><font color='#3366FF'><b>Trading Code:</b></font></td>
      <td width='85%'>ISL</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News Title:</b></font></td>
      <td>DSENEWS: Withdrawal of Authorized Representative</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News:</b></font></td>
      <td align='justify'>Withdrawal of Authorized Representative: IDLC Securities Ltd., DSE TREC No. 58, has withdrawn one of its Authorized Representatives, Mr. Mohammad Ziaur Rahman, with immediate effect.</td>
   </tr>
</table>

This is my table from which I want get every table trading code and title and store it into my database, please help to to scrapping this data.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
tapos ghosh
  • 2,114
  • 23
  • 37

1 Answers1

1
$html="<table border='0' cellspacing='3' width='100%'>
   <tr>
      <td width='15%'>Trading Code:</td>
      <td width='85%'>RCL</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News Title:</b></font></td>
      <td>DSENEWS: Withdrawal of Authorized Representative</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News:</b></font></td>
      <td align='justify'>Withdrawal of Authorized Representative: Royal Capital Ltd., DSE TREC No. 21, has withdrawn one of its Authorized Representatives, Mr. Md. Zikrul Haque, with immediate effect.</td>
   </tr>
</table>
<br>
<table border='0' cellspacing='3' width='100%'>
   <tr>
      <td width='15%'><font color='#3366FF'><b>Trading Code:</b></font></td>
      <td width='85%'>ISL</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News Title:</b></font></td>
      <td>DSENEWS: Withdrawal of Authorized Representative</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News:</b></font></td>
      <td align='justify'>Withdrawal of Authorized Representative: IDLC Securities Ltd., DSE TREC No. 58, has withdrawn one of its Authorized Representatives, Mr. Mohammad Ziaur Rahman, with immediate effect.</td>
   </tr>
</table>";




/* Construct XPath expression to find required data*/
$query='//td[contains( . , "Trading Code" )]/following-sibling::td|//td[contains( . , "News Title" )]/following-sibling::td';

/* create the DOMDocument & DOMXPath objects */
$dom=new DOMDocument;
$dom->loadHTML( $html );
$xp=new DOMXPath( $dom );

/* Run the query to find nodes */
$col=$xp->query( $query );

/* Process the nodes */
if( !empty( $col ) ){
    foreach( $col as $td ){
        /* do something with data found */
        echo $td->nodeValue;
    }
}

A useful reference when constructing XPath expressions ( & CSS selectors ) can be found here

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46