Thursday, October 22, 2009
PHP Regular expressions tool
http://gethifi.com/regexp/
http://gethifi.com/regexp/
http://gethifi.com/regexp/
Wednesday, December 19, 2007
PHP filters
Gone are days of writing secure functions for validating URL, email address,etc.
In october 2005, first version of Filter package was released by PECL and within 1 year and 5 more releases PECL released final version 0.11 of Filter pack
age.
You can download filter package from here http://pecl.php.net/package/filter.
All you need is Php5.0 or higher and PEAR installer.
Installation steps are simple just write this in your console
$ pecl install filter
Usage Documentation.
Although filter functions are documented in php.net (http://in.php.net/filter), I find these documentation incomplete.
While doing some googling I found a good link which described different usages of Filter functions in a simple manner.
The Link is : http://phpro.org/tutorials/Filtering-Data-with-PHP.html
I won't provide you with entire usage as it would be a waste of time when I already have good link doing the same for me.
I will only provide a snapshot to show you the power of PHP filters
Filter IP Address
Following on from validation of URLs, we often find we need to validate an IP Address. Of course, and IP address may be of different formats for ipv4 and ipv6. An IP address may also need to be within a range of private or reserved ranges. The filter extension makes it possible to discern these differences and to validate an IP address to fit most needs. In its simplest form the validation of a url will look like this.
/*** an IP address ***/
$ip = "192.168.0.1";
if(filter_var($ip, FILTER_VALIDATE_IP) === FALSE)
{
echo "$ip is not a valid IP";
}
else
{
echo "$ip is valid";
}
?>
As we have supplied the above with a valid IP address it validates and all is well. But now we may wish to validate an IPV6 address or an address with a private range. The IP filter has several flag with which to validate an IP address with. Listed here.
- FILTER_FLAG_IPV4
- FILTER_FLAG_IPV6
- FILTER_FLAG_NO_PRIV_RANGE
- FILTER_FLAG_NO_RES_RANGE
Starting at the top we will check to see if an IP is a valid IPV4 address.
/*** an IP address ***/
$ip = "192.168.0";
/*** try to validate as IPV4 address ***/
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE)
{
echo "$ip is not a valid IP";
}
else
{
echo "$ip is valid";
}
?>
Please visit the above link, read carefully and start saving time by using the above
Wednesday, October 24, 2007
Reduce your page load time
Ever wondered why Super sites like Yahoo, AOL, etc load faster than your site.
If you are still wondering than this small article and the link mentioned at the bottom may help you reduce your page load time.
In any optimization effort it's critical to profile current performance to identify where the greatest improvement can be made. It's clear that the place to focus for fast web pages is the front end:
1. There is more potential for improvement by focusing on the front end. Making the back-end twice as fast reduces response times by 5-10%, whereas making the front end twice as fast saves 40-45%.
2. Front end improvements typically require less time and resources than back-end performance projects.
3. Focusing on front end improvements has proven to work. Over fifty teams at Yahoo! have reduced their end-user response times by following the best practices described here.
As web applications evolve to contain more functionality and content, these best practices are expected to have an even bigger impact.
Results Php Benchmark tests:
http://www.php.lt/benchmark/phpbench.php
The above link serves only the programming aspect of PHP.
There is still lot of improvement you can do in JavaScript and DOM.
So keep googling you might end up with what you exactly want.
Friday, August 31, 2007
Send Header without error
For long I always thought that header function will only work when there is nothing rendered before the header call.
But recently I discovered(ofcorse with help of google) that we can render anything before the header call and still redirect the page.
Normal Usage with error is
echo "This will give error"
header("location:http://www.yahoo.com");
echo "This will NOT give error";
header("refresh:0; url:http://www.yahoo.com");
Here refresh:0 means the page will load after 0 seconds(which is instantly).
Basically this can be used when we want to have delay the page loading say we want to load yahoo after 2 seconds.
In this case the code will be
header("refresh:2; url:http://www.yahoo.com");
So start using this header call and redirect the page anytime during page execution.
Saturday, August 18, 2007
Store procedure usage in PHP
A stored routine is either a procedure . Stored routines are created with CREATE PROCEDURE statements. A procedure is invoked using a CALL statement, and can only pass back values using output variables. Stored routines may call other stored routines.
A stored procedure or function is associated with a particular database.
Procedures can be created using CREATE PROCEDURE command of mysql.
To know the exact syntax visit Online MySql documentation.
Once the procedure is created we have to use it in php code for this PHP's mysqli extension allows you to access the functionality provided by MySQL 4.1 and above
In order to have these functions available, you must compile PHP with support for the mysqli extension(MySQL Improved Extension).
To install the mysqli extension for PHP, use the --with-mysqli=mysql_config_path/mysql_config configuration option where mysql_config_path represents the location of the mysql_config program that comes with MySQL versions greater than 4.1.
mysqli functions are similar to mysql functions, there is substitute for each mysql functions in mysqli.
For e.g.
mysqli_query() has to be used instead of mysql_query()
using mysqli_query() the stored procedure query can be called and its result can be fetched.
Note: if you have to use any of mysqli functions then you will have to use mysqli functions throughout the current mysql connection
i.e. from mysqli_connect() to mysqli_close() all intermediate functions used must be of mysqli and not mysql.
references:
http://in.php.net/
http://dev.mysql.com/doc/refman/5.1/en/
Saturday, July 14, 2007
Although most existing PHP 4 code should work without changes in PHP5, you should pay attention to the following backward incompatible changes:
- There are some new reserved keywords.
- strrpos() and strripos() now use the entire string as a needle.
- Illegal use of string offsets causes E_ERROR instead of E_WARNING. An example illegal use is: $str = 'abc'; unset($str[0]);.
- array_merge() was changed to accept only arrays. If a non-array variable is passed, a E_WARNING will be thrown for every such parameter. Be careful because your code may start emitting E_WARNING out of the blue.
- PATH_TRANSLATED server variable is no longer set implicitly under Apache2 SAPI in contrast to the situation in PHP 4, where it is set to the same value as the SCRIPT_FILENAME server variable when it is not populated by Apache. This change was made to comply with the » CGI specification. Please refer to » bug #23610 for further information, and see also the $_SERVER['PATH_TRANSLATED'] description in the manual. This issue also affects PHP versions >= 4.3.2.
- The T_ML_COMMENT constant is no longer defined by the Tokenizer extension. If error_reporting is set to E_ALL, PHP will generate a notice. Although the T_ML_COMMENT was never used at all, it was defined in PHP 4. In both PHP 4 and PHP 5 // and /* */ are resolved as the T_COMMENT constant. However the PHPDoc style comments /** */, which starting PHP 5 are parsed by PHP, are recognized as T_DOC_COMMENT.
- $_SERVER should be populated with argc and argv if variables_order includes "S". If you have specifically configured your system to not create $_SERVER, then of course it shouldn't be there. The change was to always make argc and argv available in the CLI version regardless of the variables_order setting. As in, the CLI version will now always populate the global $argc and $argv variables.
- An object with no properties is no longer considered "empty".
- In some cases classes must be declared before use. It only happens if some of the new features of PHP 5 (such as interfaces) are used. Otherwise the behaviour is the old.
§ get_class(), get_parent_class() and get_class_methods() now return the name of the classes/methods as they were declared (case-sensitive) which may lead to problems in older scripts that rely on the previous behaviour (the class/method name was always returned lowercased). A possible solution is to search for those functions in all your scripts and use strtolower().
This case sensitivity change also applies to the magical predefined constants __CLASS__, __METHOD__, and __FUNCTION__. The values are returned exactly as they're declared (case-sensitive).
- ip2long() now returns FALSE when an invalid IP address is passed as argument to the function, and no longer -1.
- If there are functions defined in the included file, they can be used in the main file independent if they are before return() or after. If the file is included twice, PHP 5 issues fatal error because functions were already declared, while PHP 4 doesn't complain about it. It is recommended to use include_once() instead of checking if the file was already included and conditionally return inside the included file.
- include_once() and require_once() first normalize the path of included file on Windows so that including A.php and a.php include the file just once.
Reference: http://www.php.net/manual/en/migration5.incompatible.php
Thursday, May 24, 2007
Get First Buisness day of Month- PHP
This can be set in cron job and run every day to check if this is first buisness day.
This can be useful for sending newsletters / promotional offers , etc on first buisness day of every month.
function definition is given below
/**
* Function which checks if current date passed is a first buisness day of any month or NOT.
* Description - First it checks if given date is 01 if yes then check if its not sat/ sun / public holidays.
if 01st is nonbuisness day then ignore return false.
In this case when the function will be called again on 02, here it will check if 02 is buisness day, if yes then if 01 was non buisness day then 02 is definitely first buisness day of the month.
This check will be done until 06 of every month.
* @param integer $year year in YYYY format
* @param integer $month Month in MM format
* @param integer $day day in DD format
* @param mixed $non_buisness_days array containing all non buisness days values will be in format Sat, Sun, Tue
* @param mixed $public_holidays An array containing list of all holidays for given year, the dates should be in YYYY-MM-DD format
* @return bool true if given date is first buisness day else otherwise false.
* @author VN (02/05/2007)
**/
function is_first_buisness_day($year,$month,$day,$non_buisness_days='',$public_holidays='')
{
$tmpstamp = mktime(1,1,1,$month,$day,$year);
//echo $year,$month.$day."
";
$ymd = date('Y-m-d',$tmpstamp);
if($public_holidays == ''){
$public_holidays = array();
}
if($non_buisness_days == ''){
$non_buisness_days = array();
}
$d = date('d',$tmpstamp);
$day = date('D',$tmpstamp);
$month = date('m',$tmpstamp);
$year = date('Y',$tmpstamp);
$previous_d = $d-1;
$previous_day = date('D',mktime(1,1,1,$month,$previous_d,$year));
$tp_first_day = mktime(1,1,1,$month,01,$year);
$day_first_day = date('D',$tp_first_day);
$day_first_ymd = date('Y-m-d',$tp_first_day);
if(($d == 01 && (!in_array($day,$non_buisness_days) && !in_array($ymd,$public_holidays)))
($d != 01 && (in_array($day_first_day,$non_buisness_days) in_array($day_first_ymd,$public_holidays)) &&
(!in_array($day,$non_buisness_days)) && (!in_array($ymd,$public_holidays)) &&
($d <=5 && (in_array($previous_day,$non_buisness_days) (in_array($day_first_ymd,$public_holidays)))))){ $return_value = true; } else { $return_value = false; } unset($d); unset($day); unset($month); unset($year); unset($non_buisness_days); unset($previous_d); unset($previous_day); unset($tp_first_day); unset($day_first_day); unset($public_holidays); return $return_value; } Usage
$pb = array('2007-05-01','2007-12-25');
$dt = $_GET['getdate'];;
$dt = explode('-',$dt);
$non_buisness_days = array('Sat','Sun');
echo "Non Buisness days array:
";
foreach($non_buisness_days as $i => $v){
echo "'".$v."'";
echo " ";
}
echo "
";
echo "Public holiday array:
";
foreach($pb as $i => $v){
echo $v;
echo "
";
}
echo "
";
$bool = is_first_buisness_day($dt[0],$dt[1],$dt[2],$non_buisness_days,$pb);
if($bool){
echo 'YES
'.$_GET['getdate'].' is a first buisness day of Month '.date('F - Y',mktime(1,1,1,$dt[1],$dt[2],$dt[0]));
}
else{
echo 'NO
'.$_GET['getdate'].' is not a first buisness day of Month '.date('F - Y',mktime(1,1,1,$dt[1],$dt[2],$dt[0]));
}
Friday, April 20, 2007
String Manipulation . explode string
the below mentioned preg_Split() function is very useful in exploding entire string for each of its characters;
$str = 'Mumbai';
$chars = preg_split('//', $str,-1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
output
Array
(
[0] => M
[1] => u
[2] => m
[3] => b
[4] => a
[5] => i
)