Wednesday, 10 August 2016

Check CSV File is in Valid Format in PHP

Function to check whether a file is a valid CSV file. This function basically checks file for the allowed csv meme types rather checking for the content of the csv file for the format.

function isCSV($filename) {
    $allowed_mime_types = [
        'text/csv',
        'text/plain',
        'text/comma-separated-values',
        'application/vnd.ms-excel',
        'application/csv',
        'application/vnd.msexcel',
        'text/anytext',
        'application/octet-stream',
        'application/txt',
        'application/excel',
    ];
    $file_info = file_info_open(FILEINFO_MIME_TYPE);
    $file_mime_type = file_info_file($file_info, $filename);

    return in_array($file_mime_type, $allowed_mime_types);
}


Tuesday, 9 August 2016

PHP Function to Calculate Age from Date of Birth

Make use of this simple PHP function to calculate age of person from the date of birth. Find your age with this simple php function. Find the age of celebrities with this simple php function.

function calculateAge($dob) {
            $date = $this->calculateDOB($dob);
            $dateObj = new DateTime($date);
            $nowObj = new DateTime();
            $interval = $nowObj->diff($dateObj);
            return $interval->y;
 }

Monday, 8 August 2016

PHP Function to create SEO friendly URLs

Use this function to create SEO friendly urls. Make urls readable and seo friendly with this simple php function.

function seoUrlAscii($str, $replace=array(), $delimiter='-') {
            if(!empty($replace)) { $str = str_replace((array)$replace, ' ', $str); }
           
            $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
            $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
            $clean = strtolower(trim($clean, '-'));
            $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
           
            return $clean;
        }

Sunday, 7 August 2016

Check whether a word is Palindrome or not in PHP

PHP Function to test whether a given word is a palindrome or not. A Palindrome is a word which when reversed also gives the same word. This simple function helps in finding whether the given word is a palindrome or not.

function isWordPalindrome($word) {
    $word = str_replace(' ', '', $word);
    return $word == strrev($word);
}

Friday, 5 August 2016

PHP Function to Create a Directory in given Path

Simple php function to create directory in given path. This makes creating your directory easy and efficient.

function createFolder($path, $foldername) {
            $filename = $path.'/'.$foldername;
          
            if (!file_exists($filename)) {
                mkdir($path.'/'.$foldername, 0777);
                return true;
                exit;
            } else { return true; }

}

Pass your path in $path variable and the folder name in $foldername variable.

Wednesday, 3 August 2016

Generate Folder Name based on ID or Number in PHP

PHP Function to generate folder name based on id or a number. Use the following function to create folder name as desired based on your needs.

function generateFolderName($no) {
    $id = ceil(($no/1000))-1;    //Here "1000" defines the folder change after 1000 files
    $folder = "";
    while($id >= 0) {
        $folder = chr($id % 26 + 65) . $folder;
        $id = floor($id / 26) - 1;
    }
    return strtolower($folder);
}


This makes folder name change after 1000 files. Hence 1-1000 will return "a" and 1001-2000 will return "b" and hence like that.

Monday, 1 August 2016

PHP function to Download File

Download a file from given URL and store it in the specified path. This simple function takes url and path as inputs and saves the file from the url to the specified path.

We can download image files, pdf files, csv files and sql files from the specified url and store in the required path.

function downloadFile($file_url, $save_path) {
            $newfname = $
save_path;
            $file = fopen ($
file_url, 'rb');
            if($file) {
                $newfile = fopen ($newfname, 'wb');
                if($
newfile ) {
                    while(!feof($file)) {
                        fwrite($
newfile , fread($file, 1024 * 8), 1024 * 8);
                    }
                }
            }
            if($file) { fclose($file); }
            if($
newfile ) { fclose($newfile ); }
 }