During my web coding career, I’ve wound up creating a fair number of functions and library routines to make various tasks easier. The following are ones that I’ve collected together into libraries that others might find useful.
The “view source code” links on this page will send your browser a document with a Content-type: header of text/plain. This should hopefully cause your browser to display it, rather than trying to interpret it. Similarly, the “download library” links will send a Content-type of application/octet-stream, which should cause your browser to give you an option to save it. If these behaviors don’t occur, please check your browser configuration, or use some other standard mechanism (such as “right-click and Save As...”) to force the appropriate behavior.
If you want to use one of these libraries for any purpose, please see the licensing information at the bottom of this page.
This library implements an entire AJAX-based directory browser, which can be displayed on a web page using a <select> form element of whatever size you desire. The page that includes the <select> element simply needs to include a single JavaScript file; this contains routines that will contact a server-side script and retrieve directory listings. The download package includes a server-side script in PHP to handle the AJAX requests.
Functions
While there are nearly a dozen functions in this library, only two should ever be called from outside the library itself. For full descriptions and calling syntax for those two functions, use the button to the right. If you want to investigate all the library internals, please read the source code.
Note: Not all functions are described here, because most should never be called from outside the library itself. As described in the README.txt supplied as part of the download package, the dirbrowser_init() function should be called by an onLoad handler as the document loads, in order to initialize the contents of the directory browser. That browser should have an onclick handler which triggers the toggle_item() function. Other functions will be called by those two, and should not be called independently. If you want to investigate all the library internals, please read the source code.
voiddirbrowser_init ( string select_id )
Initializes the contents of the form <select> element whose ID is passed as a single argument. The <select> element should be empty before initialization; this routine will fill it with <option> elements representing the subdirectories of the initial directory (configured with the Base_Dir variable in the library). Returns void. This function should be executed by an onLoad handler attached to the document <body>.
voidtoggle_item ( string option_id )
Toggles a single “directory” (or <option> element within the dirbrowser <select> element) from open to closed or from closed to open. Operates on the element whose ID is passed as a single argument; returns false if that element doesn’t exist, or void otherwise. This function should be triggered by an onclick handler attached to the <select> element.
Usage
For full installation and usage instructions, see the README.txt file in the Zip download file, below. Abbreviated version: put the .js library and associated PHP script on your server, editing options to configure; then set up an HTML page including a form and a pair of JavaScript event handlers.
Demonstration
If you’d like, you can see a version of libdirbrowser in action. The libdirbrowser demonstration page will allow you to browse around the directories on my own personal site, and submit a value to a debugging script.
This PHP library implements a variety of routines for trying to promote natural-sounding English. It includes functions for displaying lists of things in English syntax, giving file sizes in human-readable ways, obfuscating email addresses, and other tidbits.
Functions
There are 6 functions in this library; use the button to the right in order to see full descriptions and calling syntax for each one.
This function takes a reference to an array, and returns a string that lists the array’s contents in normal English (using commas and the word “and” if needed). For example, the array (foo, bar) becomes foo and bar; (foo, bar, wombat) becomes foo, bar, and wombat. The $nocomma variable, if true, suppresses the use of the serial comma (aka the Oxford comma), providing foo, bar and wombat.
Given a string such as Shakespeare, William or Tempest, The or Midsummer Night's Dream, A, returns the proper, de-comma’d version. Makes no attempt to trap strings like Tom, Dick, and Harry unless the optional $chk_input argument is true, and doesn’t guarantee it will catch them all even in that event. If $chk_input is true and the string looks like a list, it will be returned unchanged.
stringget_ordinal ( mixed number )
Takes in a cardinal number (1, 2, 3, etc.), and returns it as an ordinal number (1st, 2nd, 3rd, etc.). In the process, any leading zeroes will be stripped. The input may be an int or a string.
stringhuman_bytes ( mixed num[, int round [, bool dec [, bool bytes [, bool pref ] ] ] ] )
Given a positive int (or a string representing a positive integer), will return a string giving a human-readable number of bytes, rounded to the most sensible prefix (kilobytes, gigabytes, whatever). Other arguments control the rounding precision, whether prefixes are calculated in binary or decimal, and just how the unit(s) should be spelled out; consult the documentation inside the function itself for details.
voidprint_obfuscated_email ( string email )
Assumes its argument is an Internet email address (username@[subhost.]hostname.tld format, not obsolete things like bang paths) and prints an obfuscated version of it designed to foil address-harvesting bots. Note that this function prints immediately, rather than returning a value. Also, it sometimes makes use of the spell_out() function, below.
stringspell_out ( string text )
Will attempt to spell out the supplied string letter by letter, for example, turning cat into see-ay-tee. Allows easy configuration of the spellings used for each letter, so you can easily have sea-aye-tea if you’d prefer.
Usage
To use functions from libenglish.php in your PHP script, include the line include(/path/to/libenglish.php) at the beginning of your script. Then simply invoke any of its functions normally.
This JavaScript library contains a subset of the functions from its PHP sister, libenglish.php (above). It also adds a couple of functions that handle things that PHP has built in.
Functions
There are 3 functions in this library; use the button to the right in order to see full descriptions and calling syntax for each one.
Takes a float, an int, or a string representing a number, and formats it with commas every three spaces. If given an optional delimiter, it will use that instead of a comma as the separator.
This function takes a reference to an array, and returns a string that lists the array’s contents in normal English (using commas and the word “and” if needed). For example, the array (foo, bar) becomes foo and bar; (foo, bar, wombat) becomes foo, bar, and wombat. The nocomma variable, if true, suppresses the use of the serial comma (aka the Oxford comma), providing foo, bar and wombat.
stringucfirst ( string text )
Returns a version of text that has the first letter converted to uppercase. Replicates the PHP or Perl ucfirst() functions.
Usage
To use functions from libenglish.js in a web page, simply link to the library with <script language="JavaScript" type="text/javascript" src="/path/to/libenglish.js"></script> in the <head> of your document. Then invoke any function normally within a JavaScript block.
This library, written in PHP, contains a pair of functions that are useful for wishing someone a “good morning”, “good evening”, or whatever, based on when the Sun actually rises and sets at a particular location.
Functions
There are 2 functions in this library; use the button to the right in order to see full descriptions and calling syntax for each one.
mixeddate_sunrise_sunset ( string rise_set[, string string_float, [, int time [, float lat [, float long ]]]] )
Based on an algorithm found at http://williams.best.vwh.net/sunrise_sunset_algorithm.htm. Calculates the time of either sunrise or sunset (as determined by the first argument) for any day (as passed in the time argument as a Unix timestamp; defaults to the current date) for the location passed as a latitude-longitude pair in the lat and long arguments (defaults to San Francisco). May return either a string (such as “6:18” or “19:32”) or a float (such as 6.3 or 19.5333), based on the string_float argument.
stringtime_of_day ( [ int timestamp ] )
Returns a plain-English description of the time of day, suitable for creating sentences like “Good morning!” or “Have a pleasant evening.” Will return one of morning, afternoon, day, evening, or night, based on the Unix timestamp passed (or the current time, by default). These are based on the actual times of sunrise and sunset for the location specified in date_sunrise_sunset() (above). For example, if the location is in the northern temperate zone, this function will return afternoon at 18:01 on June 22nd, but will return evening at 17:59 on December 22nd. This allows the computer’s estimation of “what time of day it is” to match how much light is in the sky outside a user’s window.
Usage
Include the line include(/path/to/libsuntime.php) at the beginning of your PHP script; then invoke any of its functions normally.
A single function, written in PHP, that opens a file safely for reading or writing, providing file locking and error reporting. If the file you attempt to open is locked, safeopen() will automatically retry it every X milliseconds until Y seconds have passed, then time out. You can set X and Y by altering a pair of variables declared at the beginning of the function. By default, the retry frequency is 250 milliseconds, and the final timeout is 5 seconds.
Will attempt to open the file named by $filename, then flock() it, retrying until it succeeds or the timeout delay elapses. Returns a filehandle resource on success; on failure, it will return false and put an error message into the $error_msg variable. By default, files are opened for reading only and the lock mode is exclusive (LOCK_EX). The $filemode variable will take any standard fopen() mode as its argument, and the $lockmode variable will take any standard flock() locking operation.
Lock operations automatically have LOCK_NB added to them, so it’s unnecessary to add that to the $lockmode. This allows safeopen() to abort after the timeout limit is reached, rather than blocking indefinitely.
Portability
This function has been tested and deployed on Linux, and works fine there. On Solaris, some work may be needed. At a bare minimum, you'll have to add the -l permission to the file’s mode before it can accept an exclusive lock (man -s 2 chmod or man flock for details). It has not yet been tested on FreeBSD or other Unices.
Usage
You could add this function to a library or other PHP file that you’re already include()ing in your script, or just copy-and-paste the entire thing into your script.
These libraries are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. Please feel free to study them, to use them in your non-commercial web site, and to develop other (non-commercial) libraries and applications based on them. Please don’t be too worried about the “non-commercial” aspect of this license, either. I’m not at all opposed to people using my work commercially, I’d simply like the opportunity for royalties if you think you’re going to make a lot of money. Please contact me before using these libraries commercially; I’m willing to work out very reasonable licensing terms.