PHP Logo

Creating A 404 Page In PHP

2nd April 2008 - 3 minutes read time

Setting up a 404 page on your site will help users when they navigate to a page that doesn't exist. Rather than dropping them into a scary server message you can give them a nice friendly error page. The first step is to make sure that if the user generates a 404 error they are given a nice page. Add this line to your .htaccess file.


ErrorDocument 404 404.html

Now when a user hits a non existent page they will see you nice error page. However, you sometimes will want to produce a 404 page when the server doesn't give out a 404 error, for example, if you have the following URL.

PHP Logo

Array Sorting Algorithms In PHP

1st April 2008 - 8 minutes read time

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations, especially when sorting things like dates as PHP usually guesses which value is higher than the other and can produce odd results. However, there are plenty of sorting algorithms available than can allow you to sort an array in any way you want.

The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.

PHP Logo

PHP array_merge() Function Improvement

31st March 2008 - 3 minutes read time

The array_merge() function in PHP is a handy way of adding one or more arrays together. Here is an example of how to use it.

$array1 = array(3, 21, 12); // set up first array
$array2 = array(63, 1, 9); // set up second array
$array3 = array_merge($array1, $array2); // merge arrays
print_r($array3); // print!

This will print the following.

Array
(
 [0] => 3
 [1] => 21
 [2] => 12
 [3] => 63
 [4] => 1
 [5] => 9
)

The only problem with this function is that it resets any numeric keys, so the following example would produce the wrong result.

PHP Logo

Creating A URI Slug With PHP

28th March 2008 - 3 minutes read time

The use of mod_rewrite on a site can have a powerful effect on search engine positioning, but to do it properly you will need to create a "slug" for each page. A slug is a lowercase alphanumeric version of the page title, with any spaces removed.

To get a slug you will need to use a function to turn a readable page title into a string that can be used as part of a URI.

This function is taken from Bramus and his excellent article about creating a post slug, and it does the job very nicely.

PHP Logo

PHP Browser Detection

27th March 2008 - 4 minutes read time

Retrieving the current user agent using PHP is done via the use of the $_SESSION super global array. The following line of code will print off your user agent.

echo $_SERVER['HTTP_USER_AGENT'];

For Firefox on Windows this user agent will look like this.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12

This is all fine, but what about getting more meaningful information, like just the version number. Here is a function that will get the version number (major or minor) from anyone visiting a page with Internet Explorer.

PHP Logo

PHP Easter Eggs

26th March 2008 - 2 minutes read time

There are a couple of hidden features that you can see on just about any PHP driven website. You can get either an image or a list of credits for PHP by appending one of the following strings to the end of the URL.


?=PHPE9568F34-D428-11d2-A769-00AA001ACF42
?=PHPE9568F35-D428-11d2-A769-00AA001ACF42
?=PHPE9568F36-D428-11d2-A769-00AA001ACF42
?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

Here is a quick explanation of each code.

PHP Logo

The glob() Function In PHP

25th March 2008 - 5 minutes read time

The glob() function in PHP uses simple pattern matching to find files and directories in a directory and return those file names as an array. It simplifies all of the PHP directory functions, so instead of opening the directory and then reading every file name one by one, you can just use glob() and so it in one function call. Additionally, glob() works very closely with the file system and so it very quick.

Here is a simple example.


$globOut = glob('*');

The $globOut variable now contains a list of all of the files and directories in the root folder. If you want to find all of the files ending with HTML in a directory then use the following.

The function takes another parameter which is a constant that defines some ways of changing the output. Here is a list of the available constants.

Common Regular Expressions

24th March 2008 - 2 minutes read time

Here are some of the regular expressions that I frequently use.

Find a blank line


^$

Spaces


[ \t]+

You can use this to break a text string apart into words.

Date


\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}

This will match anything in the format mm/dd/yyyy, or even dd/mm/yyyy.


[A-Z][a-z][a-z] [0-9][0-9]*, [0-9]{4}

Will match a formatted date, such as Mar 24, 2007.

Time


([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?

This will match HH:MM or HH:MM:SS or HH:MM:SS.mmm.

IP Address

MooTools Home Page

23rd March 2008 - 2 minutes read time

If you are looking for information about the MooTools JavaScript framework then your first stop should be the MooTools home page. The site not only allows you to download MooTools, but it is also a great resource for learning how to use MooTools.

MooTools JavaScript Framework

There are examples of all the things you can do on the demo page, and information about all of the classes available in the framework on the documentation pages.

RegExLib.com: The Regular Expression Library

22nd March 2008 - 3 minutes read time

Writing regular expressions can sometimes be a real pain, especially if you are not used to them. Rather than trying for yourself to make a regular expression you might want to think about looking for regular expressions that other people have made. Rather than reinventing the wheel to prove you can do something,using free third party regular expressions can save you a lot of time.

The RegExLib or regular expression library is a great resource for finding any regular expression that you are looking for.

RegExLib.com: The Regular Expression Library