Sunday, 31 July 2016

Function to make Variables safe in PHP

Use  this efficient function to make all variables safe from hacking and tampering of data . Use this basic function to validate an extra extend and increase security for PHP POST, GET and REQUEST variables.

function makeVariablesSafe($mystring) {
            $mystring= trim($mystring);
            return $mystring= mysql_real_escape_string($mystring);

}

Saturday, 30 July 2016

Convert Centimeters(cm) to Feets and Inches in PHP

Very useful php function to convert centimeter to display as Feets and inches. This function is used to calculate height of celebrities.

function cm2feet($cm) {
            $inches = $cm/2.54;
            $feet = intval($inches/12);
            $inches = $inches%12;
            return sprintf('%d \' %d "', $feet, $inches);

}

Friday, 29 July 2016

Get Short Title of the Title in PHP Function

Simple function to get subset of words from a big string. Use this simple function to trim a big sentence into a short string with subset of words.

function getTrimedSentence($string, $max_length, $offset=0, $add_dots=1){
    $words_array = explode(" ", $string);
    return
        ($add_dots && $offset>0 ? " ... " : "") .
        implode(" ", array_splice($words_array, $offset, $max_length)) .
        ($add_dots && $max_length < count($words_array) ? " ... " : "");
}

Wednesday, 27 July 2016

Encode Image in PHP as a Data Source

Use this simple php function to encode image into a data source for easy inline of the image data. This method will make the image load faster in webpage.

function encodeImage($filepath) {
    $filecontents = file_get_contents($filepath);
    $base64 = base64_encode($filecontents);
    $imagetype = exif_imagetype($filepath);
    $mime = image_type_to_mime_type($imagetype);
    return "data:$mime;base64,$base64";
}

Tuesday, 26 July 2016

HEX to RGB convertor in PHP

PHP function to convert HEX code to RGB values. This functions takes HEX code as input and returns rgb values. It can be customized to return a string of rgb values separated by comma or can be set to return rgb values as an array.

function hexToRGB($hex_code) {
    $hex_code = str_replace("#", "", $hex_code);
   
    if(strlen($hex_code) == 3) {
        $red = hexdec(substr($hex_code,0,1).substr($hex_code,0,1));
        $green = hexdec(substr($hex_code,1,1).substr($hex_code,1,1));
        $blue = hexdec(substr($hex_code,2,1).substr($hex_code,2,1));
    } else {
        $red = hexdec(substr($hex_code,0,2));
        $green = hexdec(substr($hex_code,2,2));
        $blue = hexdec(substr($hex_code,4,2));
    }
    $rgb = array($red, $green, $blue);
    //return implode(",", $rgb); // rgb values will be returned as string separated by commas
    return $rgb; // rgb values will be returned as an array
}

Monday, 25 July 2016

Remove Files Older than a date in PHP

PHP function to remove files older than number of days. This simple function takes directory path and no of days as arguments and removes the files older than particular days from the directory.

function removeFilesOlderThan($dir_path, $$no_of_days) {
    if ($handle = opendir($dir_path)) {
        while (false !== ($file = readdir($handle))) {
            $filelastmodified = filemtime($dir_path . '/' . $file);
            $filetype = filetype($dir . '/' . $file);
            if($filetype != 'file') continue;
            if((time() - $filelastmodified) > 24*60*60*$$no_of_days){
                unlink($dir_path . '/' . $file);
            }
        }
        closedir($handle);
    }
    else {
        echo "Directory not available";
    }
}


Make use of the function to remove backup files automatically after particular days in order to avoid too much of load in the server.

Saturday, 23 July 2016

PHP Function to read file to CSV array

Use these simple php function to read csv and tsv files to a php array. We can read data from a comma separated file and tab separated file and make a php array with this simple function.

function readCsvTsvToArray($filepath, $preserveKeys=false, $filetype) {
        if($filetype=="csv") $separator=",";
        else if($filetype=="tsv") $separator="  ";
        $array = array();
     
        if (!file_exists($filepath)){ return $array; }
        $fileContent = file($filepath);
     
        for ($x=0; $x<count($fileContent); $x++) {
            if (trim($fileContent[$x]) != '') {
                $line = explode("$separator", trim($fileContent[$x]));
                if ($preserveKeys) {
                    $key = array_shift($line);
                    $array[$key] = $line;
                }
                else { $array[] = $line; }
            }
        }
        return $array;
}