The story: One night I was looking over some code I had written for one of the servers at work. There was a function in particular that I did not like how it was done. Then the thought struck me to search the net to see if anyone else might have tried to do the same thing. Someone had. It did not produce output quite like I wanted, but I did like how it did its thing. So I copied and modified it. Thinking the original author might like what I had done, I posted a comment to his page (it was a blog entry) with a copy of my modified version. A couple of days later he asked me if I could post my code on my site and then he link to it from my comment. Either he liked the code or thought I could make the code display better than his blog comments could. That got me to thinking: "I bet I could whip up something to handle multiple code snippets I have done". And that is what you now behold. If you use any of this, just add link back to this page in your modified source code. At least then you will have some deniabilty.
- FormatPathsAsLinks.phps
<?php
//Will convert a path string to links for each sub-folder. See the example
// for more information
//
//USAGE: string FormatPathAsLinks( string $file_path )
//RETURNS: Returns a string containing <A HREF>'s for each path part
//DEPENDANCIES: MakeLink()
//NOTES: Should be cross-platform compliant (using DIRECTORY_SEPARATOR),
// but I don't know what might happen on Windows (with "c:\")
/******************************* EXAMPLES *******************************
<?php
echo FormatPathAsLinks('/path/file.ext') . "<br />\r";
echo FormatPathAsLinks('path/file.ext') . "<br />\r";
?>
/<a href="/path">path</a>/<a href="/path/file.ext">file.ext</a>
<a href="path">path</a>/<a href="path/file.ext">file.ext</a>
***************************************************************************/
function FormatPathAsLinks($file)
{
$RelRoot = ''; //Relative to web root setup
$return = ''; //The return var
$saved_path = ''; //Path HREF save var
//Is the file relative or absolute
if (substr($file, 0, 1) == DIRECTORY_SEPARATOR)
{
//Absolute path, strip out the web document root
$file = str_replace($_SERVER['DOCUMENT_ROOT'], '', $file);
//Set the path as absolute to web root
$RelRoot = DIRECTORY_SEPARATOR;
}
//Split the file path into an array and walk it
foreach(explode(DIRECTORY_SEPARATOR, $file) AS $data)
{
//Got data? (ie ignore the first empty element if the file path was anchored at '/')
if ($data)
{
//Save this path part for further use in the linking process
$saved_path .= $RelRoot . $data;
//Add an unlinked dir sep (not present if relative) and make a link of the path so far
$return .= $RelRoot . MakeLink($saved_path, $data);
//If this is a relative path, then this will be empty. Set it to the dir sep for the next loop
if (!$RelRoot) { $RelRoot=DIRECTORY_SEPARATOR; }
}
}
//All done.
return $return;
}
?>
- IsPlural.phps
<?php
//Simple string pluralization
//
//USAGE: string IsPlural( int NumberToCheck, string SinglularFormOfWord
// [, string PluralFormOfWord ] )
//RETURNS: the plural form of a word if the NumberToCheck is not 1
//DEPENDANCIES: <none>
//NOTES: Will add an "s" to the singluar if the plural is not passed.
// Hooray for argument defaults!
function IsPlural($num, $singular, $plural=null)
{
if ($num != 1)
{
if (is_null($plural))
{ $plural = $singular . 's'; }
return $plural;
}
else
{ return $singular; }
}
?>
- MakeLink.phps
<?php
//Simple link maker from a string
//
//USAGE: string MakeLink( string $URL_HREF [, string $link_name ] )
//RETURNS: Returns a string containing an anchor HREF
//DEPENDANCIES: ToolTips javascript library (http://www.walterzorn.com/tooltip/tooltip_e.htm),
// should ignore silently if unavailable
//NOTES: If the href is a local file or directory, MakeLink will add a
// onmouseover and onmouseout javascript event for use with ToolTips
// of the file permissions, owner name and group name. This ToolTip
// behavior may become a third optional paramter or moved into another
// function.
/******************************* EXAMPLES *******************************
<?php
echo MakeLink('/path/file.ext', 'The File') . "<br />\n";
echo MakeLink('/path/file.ext') . '<br />';
?>
<a href="/path/file.ext">The File</a><br />
<a href="/path/file.ext">/path/file.ext</a><br />
***************************************************************************/
function MakeLink($href, $name=null)
{
//Add file/folder permissions using ToolTips (http://www.walterzorn.com/tooltip/tooltip_e.htm).
GLOBAL $settings;
//$settings['tooltips'] = bool; true = yes, use tooltips
if ( $settings['tooltips'] AND file_exists($href) AND ($file_perms = stat($href)) !== FALSE )
{
//File permissions
$mouse_over = substr(sprintf('%o', $file_perms['mode']), -3) . "|";
//File owner
$user = posix_getpwuid($file_perms['uid']);
$mouse_over .= $user['name'] . "|";
//File group
$group = posix_getgrgid($file_perms['gid']);
$mouse_over .= $group['name'] . "|";
//Mod time, parsed by myDate()
$mouse_over .= myDate($file_perms['mtime']);
//Ballon ToolTips
//$mouse_over = "onmouseover=\"Tip('" . $mouse_over . '\', BALLOON, true, ABOVE, true)" onmouseout="UnTip()"';
//Regular ToolTips
$mouse_over = "onmouseover=\"Tip('" . $mouse_over . '\')" onmouseout="UnTip()"';
}
if($name==null)
{ $name=$href; }
return "<a href=\"$href\" $mouse_over>$name</a>";
}
?>
- myDate.phps
<?php
//Function to create a string of elapsed time differences
//
//USAGE: string myDate( int Unix_Epoch_Seconds [, bool AllValues ] )
//RETURNS: A string of the elapsed time relative to when the function is
// called. See examples for samples.
//DEPENDENCIES: Will use IsPlural, if available to handle pluralization.
//NOTES: Does not handle future dates at all, it will break and return
// "0 seconds". $AllValues allows one to include more time differences
// in the string. Change "$AllValues=true" to "$AllValues=false" to
// modifiy the default behavior.
/******************************* EXAMPLES *******************************
<?php
$time_to_use = strtotime("-1 year -2 months -3 days");
echo date('r', $time_to_use) . "\n";
echo myDate($time_to_use) . "\n";
echo myDate($time_to_use, false) . "\n";
?>
Sample results:
Tue, 04 Dec 2007 00:18:28 -0600
1 year, 2 months, 1 week, 4 days
1 year
***************************************************************************/
function myDate( $time, $AllValues=true )
{
//The elapsed amount of time in seconds (integers only please!)
$elapsed = time() - floor($time);
//Is there any real difference?
if ($elapsed < 1) { return '0 seconds'; }
//Setup an array of all possible time differences to check against
$times = array (
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
07 * 24 * 60 * 60 => 'week',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second' );
//Setup a return string
$returned = '';
//Loop through all of the time "constants"
foreach ($times AS $seconds => $string)
{
//Get the difference
$difference = floor($elapsed / $seconds);
//Is there an actual (positive) difference?
if ($difference >= 1)
{
//Add this difference to the return string. Will use a
// pluralization sub-function if available. Modify as desired.
if (function_exists('IsPlural'))
{ $returned .= " $difference " . IsPlural($difference, $string) . ","; }
else
{ $returned .= " $difference $string" . ($difference > 1 ? 's' : '') . ','; }
//Should we continue adding all possible differences?
if (!$AllValues) { break; }
//Subtract this difference from the total elapsed for the next loop
$elapsed -= $difference * $seconds;
}
}
//Strip the first space and final comma from the string before returning it
return substr($returned, 1, -1) . ' ago';
}
?>
- SelectTextOf.js
//Another text box contents selection script.
//
//USAGE: bool SelectTextOf( object TheObjectWithContentsToSelect )
//RETURNS: Returns true on success, false on failure
//DEPENDANCIES: none
//NOTES: I lightly searched for text box javascript selector scripts. Those
// I found all were hard-coded to select the contents of 1 element. This
// irked me. The selecting function should work for more than just one
// element. So I built this guy.
/******************************* EXAMPLES *******************************
<textarea onClick="SelectTextOf(this);">Some text to select</textarea>
***************************************************************************/
function SelectTextOf(PassedObj)
{
try
{
PassedObj.focus();
PassedObj.select();
return true;
}
catch(ErrorObj)
{
return false;
}
}
