Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * filename : KodeposRegex.php
- * desc : simply parse the kodepos site (http://kodepos.posindonesia.co.id) to retrieve its data using Regex
- * author : Tegarmaji / email: surat{at}tegarmaji.com
- * updated : unix 1342065847000
- * license : CC BY-NC-SA (http://creativecommons.org/licenses/by-nc-sa/3.0)
- **/
- ob_start();
- $search = 'pandan'; // your search "query"
- /* BEGIN request a html page, this code planned to be reconstructed using cURL */
- $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');
- /* END request, the html sourcecode should be stored on $html */
- /* BEGIN finding the data table */
- $pattern_table = '/'.
- '(<table border=0 cellspacing=1 cellpadding=2 width=98% align=\'center\' bgcolor=#b5ccc9>)'.
- '(.*?)'.
- '(<\\/table>)'.
- '/';
- preg_match($pattern_table, $html, $match_table);
- $pattern_tr = '/'.
- '(<tr bgcolor=)'.
- '("#f8f8f1"|"#eae6d0")'.
- '(.*?)(>)'.
- '(.*?)'.
- '(<\\/tr>)'.
- '/';
- preg_match_all($pattern_tr, $match_table[0], $match_tr);
- /* END find, the data table should be found & stored on $match_tr[0] */
- /* BEGIN parsing the data table on its rows & columns */
- $pattern_trtd = array(
- 'tdnum_td1' => '/<td align = "center">\d+<\\/td>( ){4}<td >/' ,
- 'td_td' => '/<\\/td>( ){4}<td >/' ,
- 'td_tr' => '/<\\/td>( ){8}<\\/tr>/' ,
- 'trbg' => '/<tr (.*?)>/' ,
- 'kel' => '/(, ){0,1}Kel(\.| ){1,2}/',
- 'kec' => '/Kec(\.| ){1,2}/',
- 'trim' => '/( ){2,}/',
- 'trimcolon' => '/((( ){1,};)|(;( ){1,}))/'
- ); // --> html tags (and some another things) to be erased/replaced
- $replace_trtd = array(
- 'tdnum_td1' => '',
- 'td_td' => ';',
- 'td_tr' => "\n",
- 'trbg' => '' ,
- 'kel' => ';Kelurahan ',
- 'kec' => ';Kecamatan ',
- 'trim' => ' ',
- 'trimcolon' => ';'
- ); // --> the replacement of html tags (and the things)
- $values = array(); // container of the data
- foreach($match_tr[0] as $trkey => $trval) {
- $values[$trkey] = explode(';', preg_replace($pattern_trtd, $replace_trtd, $trval));
- }
- /* END parse, the data should be arranged as a multidimentional array & stored on $values */
- /* BEGIN echo all the magic */
- 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>"';
- echo "<pre>\n";
- print_r($values);
- echo '</pre>';
- /* END echo */
Advertisement
Add Comment
Please, Sign In to add comment