PHP: October 2006 Archives

2006 Oct 2

I often get this error when i'm installing mysql server and running phpmyadmin. Just in case you are also experiencing the same,

Find your php.ini file. Open your httpd.conf file and look for the PHPIniDir variable. If it is set to C:\PHP then use that ini file.

Open your php.ini file on your php folder usually at C:\PHP. Look for this line


;extension=php_mysql.dll

uncomment the line by removing the semicolon ';'

Look for the loadable extension modules directory called extension_dir, set it to


extension_dir = "c:\php\ext"

You might want to enable mbstring and curl modules as well by uncommenting the following lines on your php.ini file.

extension=php_mbstring.dll extension=php_curl.dll
2006 Oct 1

when scraping websites, i usually use the function file_get_contents. However, there are times when we only need a specific portion of the site to get; for instance: getting the title of the site or the description.

Instead of using file_get_contents function we instead use the builtin file fopen and fgets functions like this:

<?php
$url = 'http://www.tildemark.com/';
$fp = fopen( $url, 'r' );          // r means open the site for reading
$buffer = trim(fgets($fp, 1024));  // read the first 1024 bytes of data
print "<pre>$buffer</pre>";
?>

But, using CURL functions will be a lot faster. We will use CURLOPT_RANGE to get the specific amount of data from a specified url. CURLOPT_RANGE defines as range(s) of data to retrieve in the format "X-Y" where X or Y are optional. HTTP transfers also support several intervals, separated with commas in the format "X-Y,N-M".

<?php
$url = 'http://www.tildemark.com/';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RANGE, "0-1024");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec ($curl);
echo "<pre>$content</pre>";
?>
2006 Oct 1

Here's a handy function to get specific tag contents. You could modify the tag you wish to scrape by changing the $start_tag and $end_tag variables. Useful in getting data from multiple html tags.

<?php
function get_tag_contents($start_tag, $end_tag, $url){
$data = file_get_contents($url);
preg_match( "|$start_tag(.*)$end_tag|s", $data, $match);
return match[1];
}
$start_tag = '<p>';
$end_tag = '</p>';
$url = 'http://www.tildemark.com/';
$tag_contents = get_tag_contents($start_tag, $end_tag);
print $tag_contents;
?>


Fell free to modify this code and don't forget to post your changes here.

About this Archive

This page is a archive of entries in the PHP category from October 2006.

PHP: September 2006 is the previous archive.

PHP: February 2007 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Recent Activity

Today

  • tildemark tweeted, "@jjdoblados SmartBro wont work with linux because the installer is an exe file. plus SMARTBRO sucks my dialup connection is even faster."
  • tildemark tweeted, "i think i accidentally drank 2 dosage of my medication. now, im feeling dizzy. is there a way i cound unswallow it?"

Monday

  • tildemark tweeted, "I'm feeling tired. i hope the clock will run a bit faster this time."
  • tildemark tweeted, "got sick for about 3 days. ended up finishing nwn2."

Thursday

  • tildemark tweeted, "im planning to move again, but i dont know where."

Sunday

  • tildemark tweeted, "I drank 3 sachets of instant coffee, ang now i cant sleep even if my eyes are sleepy? I can hear voices.wtf"

Friday

  • tildemark tweeted, "im so sleepy. Zzzzzzzz"

Sunday

  • tildemark tweeted, "some of my scipts are not working with godaddy. but works fine on the others. not mention their poorly coded admin page"

Thursday

  • tildemark tweeted, "so many pending tasks i need to finish. need more coffee !!!"
  • tildemark tweeted, "@gmtristan i dont think that is true."