Some usefull Functions in PHP

1.  This function is used to sanitize string for database security

 

function clean($str) {
  $str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}

2. This function is used to generate random password.


function genPassword($length = 8) {
    $validCharacters = "abcdefghijklmnopqrstuxyvwzABCDEFGHIJKLMNOPQRSTUXYVWZ1234567890";
    $validCharNumber = strlen($validCharacters);

    $result = "";

    for ($i = 0; $i < $length; $i++) {
        $index = mt_rand(0, $validCharNumber - 1);
        $result .= $validCharacters[$index];
    }

    return $result;
}

3. Function will return hyperlinks if we send url as a function parameter.


function urls_to_links($str) { 
    $pattern = '/((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))/is';
    $replace = '<a target="blank" href="$1">$1</a>';
    return preg_replace($pattern, $replace, $str);
}

4. This function is used to get the remote ip address

function getIpAddress()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    return $ip;
}

5. This function is used to validate the Email Address.

function check_email($email)
{
    //check for vaild email user@demain.com/co.uk/net
    if (eregi("^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$", $email)) {
           return $email;
    }

    return false;
}

6. Function used to validate string

function string_only($str)
{
    if (ereg('^[A-Za-z_][A-Za-z_]*$', $str)) {
        return $str;
    }
    return false;
}

7. Function to generate date range.


function dates_range($date1, $date2)
{
   if ($date1<$date2)
   {
       $dates_range[]=$date1;
       $date1=strtotime($date1);
       $date2=strtotime($date2);
       while ($date1!=$date2)
       {
           $date1=mktime(0, 0, 0, date("m", $date1), date("d", $date1)+1, date("Y", $date1));
           $dates_range[]=date('Y-m-d', $date1);
       }
   }
   return $dates_range;
}

echo '<pre>';
print_r(dates_range('2013-11-25', '2013-11-30'));
echo '</pre>';

It will output something like…

Array
(
    [0] => 2013-11-25
    [1] => 2013-11-26
    [2] => 2013-11-27
    [3] => 2013-11-28
    [4] => 2013-11-29
    [5] => 2013-11-30
)

Error Reporting in PHP

To enable error reporting , add the following lines to the top of your script.


error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', 1);

error_reporting function is used to report different types of PHP errrors.
We can pass different parameters to the function error_reporting.
We use these four major parameters to get the information about the error.

E_ERROR  – This is Fatal run-time errors. These indicate errors that can not be recovered , such as a memory allocation problem. Execution of the program will be stopped.
E_WARNING  – Run time warnings. Execution of the program will be stopped.
E_PARSE (integer) – This function will report Compile-time parse errors. Parse errors should only be generated by the parser.
E_NOTICE (integer) – Used to report Run-time notices. Eg. This function is used to report uninitialized variables in the program.

To turn off all the Errors you can simply use


error_reporting(0);

at the top of your php file.
Make sure that your php.ini file sets error reporting to E_ALL to output errors .

Function to convert decimal to fraction php

This is a simple function which is use to convert decimal to fraction in php.
The code for the function is given below.

 <?php
 function toFraction($number) {
 $numerator = 1;
 $denominator = 0;
 for(; $numerator < 1000; $numerator++) {
 $temp = $numerator / $number;
 if(ceil($temp) - $temp == 0) {
 $denominator = $temp;
 break;
 }
 }
 $a = ($denominator > 0) ? $numerator . '/' . $denominator : false;
 $c=array();
 $c = explode("/",$a);
 list($n, $w) = $c;
 if ($w == 1) {
 return $n /$w ;
 } else {
 return ($denominator > 0) ? $numerator . '/' . $denominator : false;
 }
 }
 echo toFraction(7.5);
 ?>

Output :
15/2

In the above function i have passed a decimal value .
The result will be the fraction form of the input.