MaggsWeb:7

www.maggsweb.com

Posts Tagged ‘ PHP ’

PHP page load time

October 31, 2011 | Comments | PHP

Display the page load time…

<?php
 
$startTime = microtime(TRUE);
$endTime = round(microtime(TRUE) - $startTime, 3) . " seconds";
 
?>

This function is used on the ADMIN sections of my own sites : http://www.maggsweb.co.uk

PHP date dashes to slashes

October 31, 2011 | Comments | PHP

Converting a date format, simply and quickly.

<?php
 
   function reverseDate($dateIn){
       list($year,$month,$day) = explode('-',$dateIn);
       return "$month/$day/$year";
   }
 
?>

This function is used on my own site : http://www.maggsweb.co.uk

display filesize

June 1, 2011 | Comments | PHP

Nicely displaying the filesize, using bytes, megabytes, etc..

<?php
 
function dispFileSize($file){
  $type = array("b", "KB", "MB", "GB", "TB", "PB");
  $pos = 0;
  $size = filesize($file);
  while ($size >= 1024) { 
     $size /= 1024; 
     $pos++; 
   }
  return round($size,1)." ".$type[$pos];
}
?>

Version them in their with their timestamp. This way, when they change, the link will change and the browser will reload the sitemap… Otherwise it will be fine to be cached as it wont have changed.

 
<link type='text/css' src='/styles/styles.css?v=<?=filemtime(DOC_ROOT."/styles_ie.css")?>' />

How to resubmit generated sitemaps automatically (using cron) or on an event (like re-generation of the sitemap).

<?php
pingSE(urlencode($url),'google');
pingSE(urlencode($url),'bing');
pingSE(urlencode($url),'ask');
?>

And the function goodiness….

<?php
 
function pingSE($sitemap,$service){
 
  switch ($service) {
    case 'bing':
      $ping = "http://www.bing.com/webmaster/ping.aspx?siteMap=$sitemap";
    break;
    case 'ask':
      $ping = "http://submissions.ask.com/ping?sitemap=$sitemap";
    break;
    case 'google':
      $ping = "http://www.google.com/webmasters/tools/ping?sitemap=$sitemap";
    break;
    default:
      return false;
}
 
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$ping);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$run = curl_exec($curl_handle);
   $response = curl_getinfo($curl_handle);
curl_close($curl_handle);
 
   if ($response['http_code'] == 200){       
       echo "Sitemap successfully submitted";       
   }
 
}
?>

Easy way of switching between mysql and “normal” dates (english, not american)…

<?php
function flipdate($dt, $seperator_in = '-', $seperator_out = '-')
{
return implode($seperator_out, array_reverse(explode($seperator_in, $dt)));
}
?>

Show Bytes

April 28, 2011 | Comments | PHP

Haven’t actually used this one, but it does work…

<?php
function size_format( $bytes, $decimals = null ) {
	$quant = array(
		// ========================= Origin ====
		'TB' => 1099511627776,  // pow( 1024, 4)
		'GB' => 1073741824,     // pow( 1024, 3)
		'MB' => 1048576,        // pow( 1024, 2)
		'kB' => 1024,           // pow( 1024, 1)
		'B ' => 1,              // pow( 1024, 0)
	);
 
	foreach ( $quant as $unit => $mag )
		if ( doubleval($bytes) >= $mag )
			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
 
	return false;
}
?>

PHP file extension

April 28, 2011 | Comments | PHP

Never used ‘pathinfo’ before, but I can see its uses..
Personally, I would explode on the ‘/’, then on the ‘.’, etc…

//PATH INFO RETURNS: DIRNAME, BASENAME, EXTENSION, FILENAME FROM A PATH
$details = pathinfo('videos/file.flv');

//EXTRACT EXTENSION FROM THE DETAILS ARRAY CREATED BY PATH INFO
$extension = $details['extension'];

//PRINT THE EXTENSION OF THE FILE
echo($extension);
?>

Dump variables

April 28, 2011 | Comments | PHP

This is my extension of an existing function, adatped for my requirements.

<?php
function dumpr($VARIABLE_NAME, $DATA){
    if (is_object($DATA)) {
        $data = (array) $DATA;
    } elseif (is_array($DATA)) {
        $data = $DATA;
    } else {
        $data = strval($DATA);
    }
    $output = "<table class='dumpr'>";
    $output .= "<head>";
    $output .= "<tr>";
    $output .= "<th colspan='2'>" . $VARIABLE_NAME . "</th>";
    $output .= "</tr>";
    $output .= "</head><body>";
    if (is_array($data)){
        foreach ($data as $key => $value) {
            //$value = dumpr($key, $value);
            $output .= "<tr>";
            $output .= "<td>$key</td>";
            $output .= "<td>$value</td>";
            $output .= "</tr>";
        }
    } else {
        $output .= "<tr>";
        $output .= "<td colspan=2>$data</td>";
        $output .= "</tr>";
    }
    $output .= "</body>";
    $output .= "</table>";
    return $output;
}
?>

Use like this:
echo dumpr(‘NAME’,$var);
echo dumpr(‘SESSION’,$_SESSION);
etc..

Displays a nicely formatted table, with values of a string / array / object clearly displayed.

Remove all non alpha-numeric characters from a string.

<?php
    function alphanumericAndSpace( $string )
    {
        return preg_replace('/[^a-zA-Z0-9\s]/', '', $string);
    }
?>