2009/08/25

Trim in perl

Perl does not come with trim function, but you can crate one easily:

# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
    my $string = shift;
    $string =~ s/\s+$//;
    return $string;
}

PHP4 CLASS VS PHP5

  1. PHP4 classes don't have scope operators such as private, public or
    protected.
  2. PHP4 constructional function should be the same name with that of the class. __construction won't work.
  3. Class variables in PHP4 classes are declared with the keyword 'var' rather
    than private or public.

REF: kavoir.com

2009/07/16

Amazon S3 upload issue

I got this error when I try to upload file to Amazon using S3.php library.

Amazon S3 Server.RequestTimeTooSkewed(403): The difference between the request time and the current time is too large.

The problem is that the developer box was down in the past few days. It is up now, but the date time is wrong.

Here is a solution:

ntpdate pool.ntp.org

2009/07/07

Call Member function

PHP5 is OO language. For a basic usage, we may not need to understand the concept of OO design in details, so sometimes we may forget how to call a member function defined in a Class. :-(

Got this error this morning:

PHP Fatal error:  Call to undefined function get_tokens()

Haha!

The solution:

$this->get_tokens();

Remember to you must have $this-> to call a member function defined in a Class.

2009/07/01

Permission denied to get property HTMLDivElement.parentNode

pForm is cool, and I just used it to build a form yesterday. But it worked fine yesterday, but this morning, when I when tried to select the date from Date field, I got this error message:

Permission denied to get property HTMLDivElement.parentNode

pForm integrated the calendar.js developed by Dynarch.com, seems this is a bug of firefox . Here is a quick fix. Open calendar.js at line 131 in the code. Add try and catch like below,

Calendar.isRelated = function (el, evt) {
   var related = evt.relatedTarget;try{
   if (!related) {
      var type = evt.type;
      if (type == "mouseover") {
         related = evt.fromElement;
      } else if (type == "mouseout") {
         related = evt.toElement;
      }
   }
   while (related) {
      if (related == el) {
         return true;
      }
      related = related.parentNode;
   }
   return false;
   }catch(e){
      return;
   }
};

2009/06/30

$_SERVER to get current URL

Checked PHP Manual,

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

If you are using apache, check this page,

SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html
SCRIPT_FILENAME=/u/rse/.www/index.html
SCRIPT_URL=/u/rse/
SCRIPT_URI=http://en1.engelschall.com/u/rse/

In my server, I can use below code to get URL without query string (or ‘?q=’).

$url = getenv("SCRIPT_URI");
//or
$url = $_SERVER['SCRIPT_URI'];

2009/06/25

include library files

If you hosted your site somewhere, your probably wanted to know where the root directory path is, in order to include (or require) some library files, which requires a whole path.

If you hosted on linux system, then it is easy to find it out, by

$docRoot = getenv("DOCUMENT_ROOT");
include ($docRoot . "path/to/yourfile");

However, if you hosted on IIS system, then getenv would not work. Here is a workaround:

require_once(dirname(__FILE__) . '/../includes/utils.php');
require_once(dirname(__FILE__) . '/includes/utils.php');
Note: direname(__FILE__)return the current dir name, so you need to tweak it a bit (for example, ‘..’ means jump up a level) to find out a correct library files.