Ordinal Suffix
This function will append the ordinal suffix to a number. If you need this for dates, the PHP date() function has an option for this. For all other times you need to supply add st, nd, rd, th.
All UK Counties
Recently, a PHPRO.ORG user requested a database of all the Counties in the UK. Not towns or cities, just the Counties. This database dump provides a table of all Counties in the UK. Ideal for dropdown menus or any other use.
PHP Finger Client Linux Kernel
This example shows how to create a simple finger client with php. The script connects to kernel.org to fetch the latest linux kernel information. Here a simple unordered list is created to display the contents. Handy for displaying the latest kernel updates on you website
Unicode Chart
Ever needed to generate a unicode table? Me either, but a PHPRO.ORG visitor asked how it might be done, here is one of a many possibilities
Get Relative Root
PHP provides functions to get the webserver document root within the $_SERVER super global array, and for getting the current working directory with the getcwd() function. But it often occurs that the document is being served from another directory within the web tree. This function will get the current working directory, relative to the web server document root.
Substr In Array
To find if a value exists in an array, the PHP in_array() function works quite nicely. But there are times when only a partial match is required to check in the array. This substr_in_array() function checks will search the values of an array for a substring. The $needl can be a string or an array of strings to search for
Convert Numbers To Roman Numerals
Roman numerals can add a bit of pizzaz to a site or be used in legal documents or even for giving a site a touch of gladitorial grace. This function will convert a number to a roman numeral that can be used anywhere on a site.'
Atomic Time
Here is a function to fetch the Atomic Time from an online atomic clock. When the right time is crucial in an application, and the time on the server clock cannot be trusted or is incorrect, this function will provide assurance the the time is correct.
Rainbow Text
A litte helper function to make your site emo2.0 compliant. This site will convert text into rainbow text. The text is colored in various colors to give swirling colored effect if used in long text strings.
Find Position Of Nth Occurrence Of String
The PHP functions that search strings do a fine job of finding the first of last occurance of a search string. But what if the task was to find the second or third occuance of a string within a string. This function provides an offset parameter to find the Nth occurance of a string within a string.
Get Domain Name From URL
This function will extract the domain name from a URL. The url can be any valid URL containg the scheme and hostname. The function will discard other information and return only the http://www.phpro.org portion of the URL string.
Alternating Row Colors
Displaying tabular data from arrays and database results often make use of a table with alternating row colors. This simple example shows how alternating row colors can be used to make every second table row a different color.
Google Maps With PHP And Phproogle
The PHPRO phproogle class allows PHP developers to quickly create Google Maps without needing to bother with the mind boggling Google Maps API. This class allows developers to create a Google Map using on PHP and the phproogle class will take care of the rest
Recursively Rename All Files In Directory With RecursiveDirectoryIterator
The PHP SPL Iterators are used in this function to efficiently recurse through a directory and rename the files within. By using the RecursiveDirectoryIterator and the RecursiveIteratorIterator the recursion is done by PHP and there is no need to be writing recursive functions within the code base.
Get Text Between Tags
PHPRO.ORG recieves many requests for solutions to every day problems. The most popular request is to find the text between two tags. These may be HTML body tags or XML tags or other. This function will get the text between tags of any named tag. This enables the user to specify any tag and the function will return the text inside.
Convert Photoshop PSD File With Imagick
Sometimes a user needs up upload a photoshop PSD file to a site. With the PHP Imagick extension the power of ImageMagick can be employed to convert the photoshop PSD file into a JPG, PNG or any other image format supported by ImageMagick.
Parse HTML With PHP And DOM
For many, the process of parsing varibles out of HTML tables has been a hard grind with numerous regular expressions and string functions. This example code shows how to parse HTML with PHP using the DOM extension to gain the values of data stored within the HTML table.
First Day Of Month
The first day of the month function does exactly as you would expect, it fetches the first day of the month. The default behavior is to default to the current month, but an optional parameter allows for fetching of the first day of any month, any time.
Strip Single Tag
The PHP strip_tags function allows for the stripping of tags and excludes the ones to be maintained. This function allows the user to specify exactly which tags to be stripped, and all others are maintained. The option is available to strip a single HTML or XML tag also. Very handy if stripping of only a single, or more tags is required.
Days Of Week Dropdown
A dropdown menu list for use in forms or calander class or wherever days of the week are required. The days and numbers have been assigned as per ISO-8601 to give the greatest compatibility with other calendar systems.
Month Dropdown List
Here is a function to create an HTML dropdown list of months. Handy for use in form classes or any script where you need to display a dropdown month list like a calander class. Two options are provided for this function, demonstrating different approaches and different speeds.
Days Between Two Dates
This function provides a simple and effective method of counting the number of days between two dates. An option is also provided to return partial days also. This function does not calculate years or months or seconds, only days. Ideal for countdown scripts and the like.
Validate Email By Regular Expression
Latter day PHP versions have the benifit of validating email address using the filter_var function from the filter extension. For those who dont have access to filter var, or simply prefer to do things with a regular expression, this function will validate most email addresses using a simple regular expression.
Recursive In Array
Recursively search a multi-dimensional array for any occurrance of a string. This function works in the same way as the PHP function in_array except this function works on single dimensional or multi-dimensional arrays. An option is also provided to check for type.
Create Columns From Array
Creating columns from an array or from a database result set can seem a little tricky. The key to making it happen is the PHP modulo operator. With some simple math, database result sets and arrays can be rendered in two, three, four or more columns.
Convert Seconds To Words
This function takes a UNIX TIMESTAMP as generated by strtotime or from any source and converts it into a human readable notation. Convert seconds to words is a function that should be done at the application level and not from from SQL queries as this may interfere with display logic.
Create-Test-Database
Here is a small but useful script to create test data for databases. Two test tables are created with PDO and filled with various data types including Binary (BLOB) data.
80000 Word List
Some times you just need a list of words, a big list. Here is a list of about eighty thousand words to help with.. well whatever!
Radians To Degrees
This function complements the Degrees to Radians functions by doing exactly the opposite, converting radians to degrees. Handy for those geographical calculations need when plotting points on a map with with online applications such as google maps or yahoo maps.
Degrees To Radians
Here is a handy function for those messing with goe location and geo targetting or other geo graphical toys. This function convert degrees to radians for use in map plotting etc.
Get Riemann Distance
This function will calculate the distance between two points on the earth using the great circle distance formulae. This function should not be used to calculate linear distances, instead use the GetLinearDistance function. This function will return a value in miles, nautical miles or kilometers
Get Linear Distance
This function calculates the linear distance between two points. Give to x1 y1 x2 and y2. This is not a function for calculating distances between latitudes and longitutes, for that purpose see the Get Riemann Distance function.
US Cities Zip Codes
Ever needed a comprehensive list of US cities, zip codes, latitude and longitude? Well you can pay up to $100 dollars for one or you can use this database of over forty one thousand (41000) cities and their locations. Great for geo targetting and geo location in your next application.
Get Full URL
A PHP function to get the full url of a web page. This function gathers several of the PHP super globals to create the whole URL.
Get Link Text
Ever wanted to get the text component of a URL. Not the link destination but the link itself. Well you search is over because the work has been done for you.
Calculate How Many Years Old
This function will work out how many years old you are based on a date provided in any format that is readable by strtotime. The function itself returns an integer of how many year old minus months day etc.
Convert BMP to JPG
The PHP GD library lacks the ability to convert BMP images to JPG. This can be achieved with the Imagick extension but for those folks with only GD at their disposal, this function will get you through the day.
Convert Unix Timestamp To MySQL Timestamp
Here is a quick but useful function to convert PHP time to MySQL times. That is, UNIX TIMESTAMP to MySQL TIMESTAMP format. Handy for all those times you need to deal with changing date formats.
Create Horizontal Gradient With PHP Imagick
The PHP Imagick extension allows for the creation of gradients. By default these gradients are vertical. This example code provides a method of creating horizontal gradients using simple rotation.
Password Strength Tester
PHP Security is an important part of creating forms. Recently there has been a proliferation of password strength testing scripts about, each with their own algorithms for checking the strength of passwords. This example shows how it is done.
Twenty Four Hour to Twelve Hour
Converting 24 hour time to 12 hour can be a hassle when you have different formats. This helper function takes 24 hour times and converts them to its 12 hour counterpart. Fun for all ages.
58000 Words
58000 Words is a MySQL table containing just what it says, over 58000 words in a MySQL dump so you can stick it directly into your database for use in your applications. Great for any time you need a word list to match against.
Directory-Size
When you need to get the size of an entire directory using PHP this function will give you result in Bytes. You can then do the math to format it into KB or MB or whatever format you like.',
PDO to Array
Here is a small utility function that takes the result of a PDO query using a key value pair and creates a single dimensional array from it.
Imagick Thumbnail From Center
This thumbnailer script is a little different from many by allowing the user to crop from the center of an image instead of the everyday resizing that pollutes the net
PHP Xajax Loading Message
This example shows how to use PHP and Xajax to create a Loading message while a server task is being processed. Handy for those occasions when you do not need a progress bar. More on the progress bar later.
PHP Xajax Accordian
This PHP Xajax script builds on the PHP Xajax Sliding Draw example and creates a great effect without any more knowledge of javascript than your socks.
PHP Xajax Sliding Draw
Ever needed a naf slideout draw to show and hide content but did not know enough javascript to get past hello world? me either, but with a little Xajax and PHP the job is little more than a chore.
Change Style with Xajax and PHP
For those new to xajax and PHP, here is a simple script to get you started. It shows how to change a divs style and content using xajax to call a PHP function.
Get All URLs From Page
I needed to get all the urls from a string. So I came up with this little function to do that. It will also get all the urls from a web page if you so desire.
wordbreak
Here is a useful PHP script that breaks a string at the last whole word before a specified number of characters. Great for news snippets or forums where you have a read more link etc.
Insert At Array Index
Here is a little helper function if you need to insert an array at a given index. Very useful when creating stuctured data for use in XML or CSV files, or any structure.
Flatten Array
This little helper is just what you need to flatten a multidensional array into a single level.
Count Lines in File
Often times we need to count the number of lines in a file. There is a right way and wrong way of achieving this goal. This is one of the right ways.
Array Values Similar
When the time comes that you need to be sure all the values of an array are identical, this little function will get you on the way.
Sanitize Email
As the internet expands, so do the amount of contact forms. Quite often these contact forms are exploited by spammers to send out millions of mails every year. Here we see how to make sure your contact form is not one of them.
First Words
Here is a NAF script to grab the first n number of words in a string. The number of words is variable to suit you application.
Lower Case an Array
Ever needed to change the case of all the elements in an array? This function shows how it is done.
Format Date
As a request to a phPro user, this function was created to format a UNIX timestamp with the default value as the current timestamp, then set the default timezone also.
Country Array
This handy array provides a list of country names and thier two letter country code as the array keys. Keep this one bookmarked.






