Archive for August, 2005

List contents of a directory

A quick and dirty way to list the contents of a directory. You might want to add more properties to it for displaying only certain file types and so on. I might post that later on.

PHP:
  1. function dirList($directory) {
  2.   $results = array();
  3.   $handler = opendir($directory);
  4.   while ($file = readdir($handler)) {
  5.     if ($file != '.' && $file != '..')
  6.       $results[] = $file;
  7.   }
  8.   closedir($handler);
  9.   return $results;
  10. }

And how to use it:

PHP:
  1. $arr = dirList("./");
  2. for ($i=0; $i<count ($arr); $i++) {
  3.   echo '<a href="'.$arr[$i].'">'.$arr[$i].'</a><br />';
  4. }

Validating an email address

Today I will present you with the devious process to validate an email address. It's basicly the same way as with the URL, just a different regular expression.

PHP:
  1. function isValidEmail($email) {
  2.   if (!eregi("^([a-zA-Z0-9\._\-]+)@([a-zA-Z0-9\._\-]+)\\.([a-zA-Z]+)$", $email)) {
  3.     return false;
  4.   } else {
  5.     return true;
  6.   }
  7. }

Posted in CSS/HTML | No Comments

Retreive age from dob

This function retreives age from date of birth. Pass it your date of birth as an integer (19800820) and it will return the age.

PHP:
  1. function DOB2Age($dob) {
  2.   $year = date('Y');
  3.   $month = date('m');
  4.   $day = date('d');
  5.    
  6.   $age = ($year - (substr($dob, 0, 4) + 1));
  7.    
  8.   if ($month == substr($dob, 4, 2)) {
  9.     if ($day>= substr($dob, 6, 2)) {
  10.       $age++;
  11.     }
  12.   } else if ($month> substr($dob, 4, 2)) {
  13.     $age++;
  14.   }
  15.   if ($age == date('Y')) {
  16.     $age = 0;
  17.   }
  18.   return $age;
  19. }

Simple photogallery script

It's all done by simply showing the contents of a directory. I've sorted a gallery in 1 folder with 3 subfolders. The name of the folder is the name of the gallery and the 3 subfolders are called "small", "big" and "huge".
(more...)

Posted in CSS/HTML | No Comments

Validating URL

I will continue to post handy scripts here in this new area called "Use the source". Since I'm not made of scripts I'm going to start by posting one script or function everyday and see how long I can keep up :wink:
Today it's a script to validate URL's. If the string is a valid URL i will return True, if it's not, False. Quite simple.

PHP:
  1. function isValidURL($url) {
  2.   if (!eregi("^((ht|f)tp://)((([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([0-9]{1,3}\.){3}([0-9]{1,3})))((/|\?)[a-z0-9~#%&'_\+=:\?\.-]*)*)$", $url)) {
  3.     return false;
  4.   } else {
  5.     return true;
  6.   }
  7. }