reWork: A Regular Expression Workbench

21st March 2008 - 3 minutes read time

Regular expressions are a very useful tool for any programmer wanting to validate input, format strings, change words, reformat data or even split apart a string into an array. However, when you are starting out, writing them it can be hard going, they are not very easy to learn and the only way to really understand them is to practice, practice, practice.

This is where reWork steps in. It is a fully functional online regular expression workbench that will allow you to plug the expression and the text in one end, and it will show you exactly what is being matched. This simple JavaScript program is far better than any stand alone application I have seen and has more functionality than you could even think about.

reWork: Regular Expression Workbench

MySQL Logo

Finding The Latest Timestamp Value From A Set Of Values In MySQL

20th March 2008 - 3 minutes read time

Storing sets of data with a timestamp is common practice, especially if you want to keep a history of the values that have been stored previously. Lets say you wanted to store a piece of information about two variables, each of which can have a history. Here is an example dataset.

PHP Logo

Save Browser Output To A File With PHP Output Buffering Functions

19th March 2008 - 6 minutes read time

The PHP output buffering functions provide a handy way of intercepting the contents of the buffer before it is sent to the browser. The output is whatever is sent to the browser whenever you print something off. PHP allows you to capture this output in a buffer before it is sent to the browser.

Output buffering is controlled by two mechanisms. The first is through the php.ini directive output_buffering, which is usually set to off. It can be turned on by setting this to either on, or the number of bytes that the buffer can take up. When this byte allocation is full the output is sent to the browser.

PHP Logo

A Garbage Collection Mechanism In PHP

18th March 2008 - 3 minutes read time

Garbage collection is a term for a maintenance function in a class or script that you don't want to run every time the script is run.  The main function of the script is to clean up anything that the script has used previously, but is now not important in the general running of the system and can be removed with no ill effects.  However, it is important that the garbage collection is not run every time the script is run as it may have a detrimental effect on the speed of the system.  To get around this we can use a random number generator to generate a number within a range, and use this to test if the garbage collection function should be run.  Here is the code.

PHP Logo

17th March 2008 - 3 minutes read time

By far the best resource for finding information about PHP and all of the functions available is from the PHP website. Not only can you view the PHP documentation, but you can also download PHP and many of the extensions like the Smarty template system.

Each PHP function and section has its own page with lots of detailed information about usage and instillation, which can be found quite easily on the site by entering the domain name followed by the function name you want to look up. If the function isn't found that the site points you towards a search results page.

PHP Logo

PHP5 Filter Functions Part 2

15th March 2008 - 6 minutes read time

Following on from the previous post about the PHP filter functions there are two more filter functions that require some extra explanation. These functions are filter_var_array() and filter_input_array().

They work in much the same way as filter_var() and filter_input() but they accept an array as the input. This enables you to sanitize or validate many different variables at the same time.

The first step in using these functions is to create an argument array. This is an associative array of data identifiers that allow you to set filter and sanitizer flags for different values. For example, assume that the following array is going to be used.

PHP Logo

PHP5 Filter Functions Part 1

14th March 2008 - 5 minutes read time

The filter functions are part of the PECL library and should come as standard on most PHP 5 installs. If they aren't there then ask your server administrator to install them.

The filter functions where created to avoid developers having to write lots of unmaintainable code in order to check the validity of variables and to sanitize these variables once validated. So rather than using many different functions and regular expressions to tell if a value is a number, a boolean or even a URL, you can just use these filter fucntions.

The main functions that you might be interested in are filter_var() and filter_input(). The filter_var() function is used to validate a single input, the parameters are:

JavaScript Logo

Using JavaScript To Run Form Events

13th March 2008 - 3 minutes read time

Creating a form as part of an AJAX control is a common practice, but when you include a submit button within the form the default behaviour of the form is to redirect to the destination supplied in action. To run a form using JavaScript you need to include two things.

<form action="javascript:return false;" method="post" id="analysisForm" >

In order to overwrite the normal operation of a form you will need to replace the action attribute with some JavaScript code that returns false. This is can be done in the form of come in-line JavaScript code (commonly called a bookmarklet) in the place of any URL in the action attribute of the form.

Next, in order to actually get JavaScript to intercept the form submission action an onsubmit event is added to the form tag.

PHP Logo

PHP Page Redirection

12th March 2008 - 3 minutes read time

To redirect to a different page using PHP you can use the header() function with the parameter 'Location: ' and the destination of the redirect.


However, if any headers are sent before this function call then the script will fail. To get around this you can either ensure that nothing is printed out on the page, or if that is not possible for some reason then you can use a JavaScript redirect. The headers_sent() function will allow you to see if any headers have been sent to the browser yet, if they have then you will need to use the JavaScript redirect like this:

PHP Logo

PHP Function To Work Out Average Values In Array

11th March 2008 - 3 minutes read time

Working out the average of a bunch of values is quite a common task, but rather than looping through the array, adding together values as you go and the using the count() function to find out the average at the end.

function average($array)
 $total = 0;
 foreach ($array as $item) {
  $total += $item;
 return $total/count($array);	

However, a much simpler way of doing things is just to use the PHP function array_sum(), which adds up all of the values in the array. Because this is done by the PHP engine it should take less time than using a for loop.

function average($array) {
 return array_sum($array) / count($array);

Tests show that the second function is only just faster when using short (2 or 3 items) arrays, but the second function is significantly faster when looking at longer (10 or more) arrays.