Tegarmaji

KodeposRegex.php

Jul 11th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. <?php
  2. /*
  3.  * filename : KodeposRegex.php
  4.  * desc     : simply parse the kodepos site (http://kodepos.posindonesia.co.id) to retrieve its data using Regex
  5.  * author   : Tegarmaji / email: surat{at}tegarmaji.com
  6.  * updated  : unix 1342065847000
  7.  * license  : CC BY-NC-SA (http://creativecommons.org/licenses/by-nc-sa/3.0)
  8. **/
  9.  
  10. ob_start();
  11. $search = 'pandan'; // your search "query"
  12.  
  13. /* BEGIN request a html page, this code planned to be reconstructed using cURL */
  14.     $html = file_get_contents('http://kodepos.posindonesia.co.id/index.php?act=searchadv&PageNo=1&kw='.$search.'&f=35&w=Jln.&k=sebagian&t=SEMARANG');
  15. /* END request, the html sourcecode should be stored on $html */
  16.  
  17. /* BEGIN finding the data table */
  18.     $pattern_table = '/'.
  19.     '(<table border=0 cellspacing=1 cellpadding=2 width=98% align=\'center\' bgcolor=#b5ccc9>)'.
  20.     '(.*?)'.
  21.     '(<\\/table>)'.
  22.     '/';
  23.     preg_match($pattern_table, $html, $match_table);
  24.     $pattern_tr = '/'.
  25.     '(<tr bgcolor=)'.
  26.     '("#f8f8f1"|"#eae6d0")'.
  27.     '(.*?)(>)'.
  28.     '(.*?)'.
  29.     '(<\\/tr>)'.
  30.     '/';
  31.     preg_match_all($pattern_tr, $match_table[0], $match_tr);
  32.    
  33. /* END find, the data table should be found & stored on $match_tr[0] */
  34.  
  35. /* BEGIN parsing the data table on its rows & columns */
  36.     $pattern_trtd = array(
  37.         'tdnum_td1' => '/<td align = "center">\d+<\\/td>( ){4}<td >/' ,
  38.         'td_td'     => '/<\\/td>( ){4}<td >/' ,
  39.         'td_tr'     => '/<\\/td>( ){8}<\\/tr>/' ,
  40.         'trbg'      => '/<tr (.*?)>/' ,
  41.         'kel'       => '/(, ){0,1}Kel(\.| ){1,2}/',
  42.         'kec'       => '/Kec(\.| ){1,2}/',
  43.         'trim'      => '/( ){2,}/',
  44.         'trimcolon' => '/((( ){1,};)|(;( ){1,}))/'
  45.     ); // --> html tags (and some another things) to be erased/replaced
  46.    
  47.     $replace_trtd = array(
  48.         'tdnum_td1' => '',
  49.         'td_td'     => ';',
  50.         'td_tr'     => "\n",
  51.         'trbg'      => '' ,
  52.         'kel'       => ';Kelurahan ',
  53.         'kec'       => ';Kecamatan ',
  54.         'trim'      => ' ',
  55.         'trimcolon' => ';'
  56.     ); // --> the replacement of html tags (and the things)
  57.  
  58.     $values = array(); // container of the data
  59.     foreach($match_tr[0] as $trkey => $trval) {
  60.         $values[$trkey] = explode(';', preg_replace($pattern_trtd, $replace_trtd, $trval));
  61.     }
  62. /* END parse, the data should be arranged as a multidimentional array & stored on $values */
  63.  
  64. /* BEGIN echo all the magic */
  65.     echo '"<a href="http://kodepos.posindonesia.co.id/index.php?act=searchadv&PageNo=1&kw='.$search.'&f=35&w=Jln.&k=sebagian&t=SEMARANG">'.$search.'</a>"';
  66.     echo "<pre>\n";
  67.     print_r($values);
  68.     echo '</pre>';
  69. /* END echo */
Advertisement
Add Comment
Please, Sign In to add comment