September 21, 2016

How to get latitude & longitude from address or postcode string

function address_to_lat_lng($address){ $ret = array(); $address = urlencode($address); $url = “http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false”; //pree($url); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_PROXYPORT, 3128); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($ch); curl_close($ch); //pree($response); $response_a = json_decode($response); $ret[‘lat’] = $response_a->results[0]->geometry->location->lat; $ret[‘lng’] = $response_a->results[0]->geometry->location->lng; return $ret; }

May 16, 2016

PHP Function ksort to recursive_ksort

PHP Function ksort to recursive_ksort function recursive_ksort(&$array) {     if (is_array($array)) {         ksort($array);         array_walk($array, ‘recursive_ksort’);     } } An important thing in this function is variable by reference instead of returning value each time. So its easy to understand for those guys who worked in C language and a little difficult for routine […]

June 28, 2015

How to remove a directory which is not empty?

How to remove a directory which is not empty? function force_rmdir($dir) { if (is_dir($dir)) { $paths = scandir($dir); foreach ($paths as $path) { if ($path != “.” && $path != “..”) { if (filetype($dir.”/”.$path) == “dir”) force_rmdir($dir.”/”.$path); else unlink($dir.”/”.$path); } } reset($paths); rmdir($dir); } }

May 13, 2015

php function to get first letter of each word and make a unique key

A PHP helper function to arrange and rearrange randomly generate php array elements.

May 7, 2014

Retrieve remote file size

Retrieve remote file size function retrieve_remote_file_size($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_NOBODY, TRUE); $data = curl_exec($ch); $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); curl_close($ch); return formatSizeUnits($size); } function formatSizeUnits($bytes) { if ($bytes >= 1073741824) { $bytes = number_format($bytes / 1073741824, 2) . ‘ GB’; } elseif ($bytes >= 1048576) { $bytes = number_format($bytes […]

April 17, 2014

How to check parent function from the function called?

How to check parent function from the function called? function backtrace($i=1){ $callers=debug_backtrace(); return $callers[$i][‘function’]; }

March 1, 2014

How to define your SITE_URL?

How to define your SITE_URL? $self = explode(‘/’, $_SERVER[‘PHP_SELF’]); array_pop($self); define(‘SITE_URL’, ‘http://’.$_SERVER[‘HTTP_HOST’].implode(‘/’, $self).’/’); define(‘IMG’, SITE_URL.’assets/uploads/’);

February 12, 2014

PHP Helper Function: get_function_static()

PHP Helper Function: get_function_static() Description: This function returns the name of calling function. You can specify the $index param to retrieve nth level of calling functions. function get_function_static($bt, $index=0) { if (isset($bt[$index][‘function’])) return $bt[$index][‘function’]; else return false; } Usage $bt = debug_backtrace(); $parent_function = $this->get_function_static($bt, 1);