MaggsWeb:7

www.maggsweb.com

Posts Tagged ‘ function ’

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];
}
?>

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;
}
?>

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);
    }
?>

Automated Tiny URL

September 24, 2010 | Comments | PHP

Function to create TinyURL’s on-the-fly
http://tinyurl.com/

[More]

I hate compressing CSS files as they become totally unreadable for editing. I end up keeping 2 versions, compressed and uncompressed, etc.. But how about compressing all your CSS files into one file, ‘on-the-fly’. Now that has solved the problem.

[More]

Just a little script I wrote to work out the date of the first day of the week.
[More]