10 Useful PHP Functions

This post provide some useful PHP functions that can be a real time saver.

Some Useful PHP Functions

Display Facebook fans number using FQL






function facebook_fans_number($page_id) {
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT fan_count FROM page WHERE page_id=".$page_id."");
return $xml->page->fan_count;
}

Display Facebook fans number Using Open Graph API








function facebook_fans_number($page_id) {
$fbpageurl = 'https://graph.facebook.com/'.$page_id;
$json = file_get_contents($fbpageurl,0,null,null);
$json_output = json_decode($json);
return $json_output->likes;
}

Display Alexa Rank for a Website










function alexa_rank($url){
$xml = simplexml_load_file("http://data.alexa.com/data?cli=10&url=".$url);
if(isset($xml->SD)) {
return $xml->SD->POPULARITY->attributes()->TEXT;
} else {
return false;
}
}

Display The Current Page URL



















function current_page_url() 
{
 $pageurl = 'http';
 if (isset($_SERVER["HTTPS"] 
)) {
 $https = $_SERVER["HTTPS"] 



; 
 }
 if (isset($https) AND $https == "on") {$pageurl .= "s";} else {$pageurl .= "";}
 $pageurl .= "://";
 if ($_SERVER["SERVER_PORT"] 
 != "80") {
  $pageurl .= $_SERVER["SERVER_NAME"] .":".$_SERVER["SERVER_PORT"] .$_SERVER["REQUEST_URI"] 

;
 } else {
  $pageurl .= $_SERVER["SERVER_NAME"] .$_SERVER["REQUEST_URI"] 


;
 }
 return $pageurl;
}

Display The Real IP Address



















function get_real_ip()
{
    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;
}

Generate Random String
















function generate_random_string($length) {
    $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz';
    $string = '';    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))] 



;
    }

    return $string;
}

Display domain favicon using PHP and Google






function display_favicon($url){
  $url = str_replace("http://",'',$url);
  return "http://www.google.com/s2/favicons?domain=".$url;
}

Get File Extension





function get_file_ext($file){
  return strtolower(substr(strrchr($file,'.'),1));
}

Remove Folder With its content














function recursive_remove_folder($folder){
         if(is_file($folder)){
             return @unlink($folder);
         }
         elseif(is_dir($folder)){
             $scan = glob(rtrim($folder,'/').'/*');
             foreach($scan as $index=>$path){
                 recursive_removee_folder($path);
             }
         return true;
        }
}

Convert Object to Array


	

	

	
	

	
	


function object_to_array($d) {
	if (is_object($d)) {
            $d = get_object_vars($d);
	}
        if (is_array($d)) {
	    return array_map(__FUNCTION__, $d);
	}
        else {
	    return $d;
	}
}

Leave a Comment

Your email address will not be published. Required fields are marked *