How to Grabbing Data from Other Website?

Lot of PHP reguler expression samples are spread over the internet. This PHP standard function having great advantages e.g.: to grab data from other websites. On this current article, I’d like to share you about how to grabbing currency rates data from Departemen Keuangan website. The script was taken from my Web Design Competition result held by Dirjen Pajak Indonesia in the end of 2006.

The PHP reguler expression preg_match function usually used to perform a reguler expression match. It searches subject for a match to the regular expression given in pattern. See the formula syntax below:

int preg_match ( string pattern, string subject [, array matches [, int flags]])

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized sub pattern, and so on. preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred. For more detailed documentation, you may take a time to read the PHP Manual or go to http://www.php.net/docs.php to get the actual version.

To improving a better looks, the string grabber function are mixed with my custom vertical marquee script. Just watch it out my complete script below:

echo("<div id='marqueecontainer' onMouseover='copyspeed=pausespeed' onMouseout='copyspeed=marqueespeed'> <div id='vmarquee' style='position: absolute; width: 98%;'>");
$data = implode('', file("http://www.depkeu.go.id/Ind/Currency/"));
$data = str_replace("</tr>", "</tr>\n", $data);
$data = str_replace("tanggal\r\n", "tanggal", $data);
$data = str_replace("<img src=../../Images/Up.gif border=0>", "<img src=images/up.gif>", $data);
$data = str_replace("<img src=../../Images/Down.gif border=0>", "<img src=images/down.gif>", $data);
preg_match("|tanggal(.*)|i", $data, $hasil);
$update_terakhir = trim($hasil[1]);
preg_match_all("|<td class=tabQuadBottom*>(.*) (.*)</td>|", $data, $hasil);
echo "<font size='1' face='Verdana, Arial, Helvetica, sans-serif' color='#666666'><strong>Update terakhir:<br></strong><i>$update_terakhir</i></font><br>";
foreach($hasil[1] as $key => $matauang)
{
//echo("$matauang--");
$tmp_matauang = str_replace(" ", "", $matauang);
$tmp_matauang = str_replace("</td><td class=tabQuadBottom>", "#", $tmp_matauang);
$tmp_matauang = str_replace("</td><td class=tabQuadBottom align=right>", "#", $tmp_matauang);
$tmp_matauang = str_replace("</td><td class=tabQuadBottom", "", $tmp_matauang);
$tmp_matauang = str_replace("<img src=images/up.gif>", "#<img src=images/up.gif>", $tmp_matauang);
$tmp_matauang = str_replace("<img src=images/down.gif>", "#<img src=images/down.gif>", $tmp_matauang);
$info = explode("#",$tmp_matauang);
$images = $info[3];
$mata_uang = $info[0];
$kurs = $info[2];
echo("$images <font size='1' face='Verdana, Arial, Helvetica, sans-serif' color='#666666'>$mata_uang = Rp. $kurs<font><br>");
}
echo("</div></div>");

The sample script above consists of several important points. The first is about initialization of the custom vertical marquee. You have to define it by your own way. Next, look at the implode function, it’s value referred to the currency rate of the Departemen Keuangan website address. Several lines after the implode function are trying to manipulating string in order to appropriating the value data to the result variabel. The first preg_match syntax in the next line actually purposed to combined the date from the original sources. The preg_match_all syntax is the core script which required to parse the row data tables page & bundled it into the result variable. The last point is try to display the result variable as much as the data grabbed by looping function.


Go to this link to see my complete page sample. Hope that this article is much useful for you.

Labels:


PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment

 

  1. Blogger student said,

    Wednesday, October 17, 2007 3:24:00 PM

    Hai, saya tertarik dengan artikel ini. Kebetulan nyasar ke website ini dengan karena emang lagi nyari script buat nge-grab data kurs dari website BI. Ehm, tapi kok link untuk ngerujuk ke artikel yang lebih lengkapnya (http://www.epsiter.org/), ga ada? Makasih ya...

  2. Blogger Eko Wahyudiharto said,

    Monday, October 22, 2007 12:16:00 PM

    Mohon maaf, alamat http://www.epsiter.org sudah tidak aktif lagi TMT juni 2007

  3. Anonymous Anonymous said,

    Sunday, March 23, 2008 7:26:00 AM

    Thank you for the information. From Santiago, Chile, South America.

    Javier Aste

  4. Blogger Eko Wahyudiharto said,

    Monday, March 24, 2008 8:41:00 AM

    Well, actually there was my latest article reviews about this right on this link

  5. Anonymous Anonymous said,

    Wednesday, March 26, 2008 11:13:00 AM

    Yang mulia Eko.
    Wah, aku jane wis kopi pasta skrip-mu je. Sayange trenyata URL file-access disabled di server hosting-ku. :(
    Tapi aku tetap klak-klik ads-mu lho, dengan harapan ben suk aku ditraktir nek kowe entuk kiriman seko mbah-mu.
    Salam dari Jogja...

  6. Blogger Eko Wahyudiharto said,

    Wednesday, March 26, 2008 11:53:00 AM

    @sing soko jogja: Modhar kowe... mangkane... pilihlah hosting yang baik & benar hue... he... he... he... he...

Post a Comment

Leave comments here...