PHP

PHP Array Of UK Counties

Use the following code to create an array of counties in the UK.

Sequentially Rename All Image Files In A Directory With PHP

The following function will rename all of the image files in a directory to be sequential. The parameters are the path of the directory that the files are in and the name of a function that will be used to sort the array of files through the PHP usort() function.

function sequentialImages($path, $sort=false) {
 $i = 1;
 $files = glob($path."/{*.gif,*.jpg,*.jpeg,*.png}",GLOB_BRACE|GLOB_NOSORT);
 
 if ( $sort !== false ) {
  usort($files, $sort);
 }
 
 $count = count($files);
 foreach ( $files as $file ) {
  $newname = str_pad($i, strlen($count)+1, '0', STR_PAD_LEFT);
  $ext = substr(strrchr($file, '.'), 1);
  $newname = $path.'/'.$newname.'.'.$ext;
  if ( $file != $newname ) {
   rename($file, $newname);  
  }
  $i++;
 }
}

The following function can be used in the second parameter to sort the files by their last modified time.

Convert A Date Into Timestamp In JavaScript

I have previously talked about using PHP to convert from a date to a time, but what about doing the same in JavaScript?

To get the unix timestamp using JavaScript you need to use the getTime() function of the build in Date object. As this returns the number of milliseconds then we must divide the number by 1000 and round it in order to get the timestamp in seconds.

Math.round(new Date().getTime()/1000);

To convert a date into a timestamp we need to use the UTC() function of the Date object. This function takes 3 required parameters and 4 optional parameters. The 3 required parameters are the year, month and day, in that order. The optional 4 parameters are the hours, minutes, seconds and milliseconds of the time, in that order.

Find Appropriate Colour In PHP

Use the following function if you want to print the text in a particular colour, depending on what the background colour is.

function opposite($color) {
  $white = 0;
  for ($i = 0; $i < 6; $i += 2) {
    $white += base_convert(substr($color,$i,2),16,10) < 127 ? 1 : 0;
  }
  return $white <= 2 ? "000000" : "ffffff";
}

It works by looking at the colour and seeing how much of it is white. If greater than two parts are significantly white then the color returned is black, otherwise white is returned.

Here are some examples to test the function out.

Turn Off Wordpress Revisions

Wordpress has a nice little revisions feature that will allow you to revert to a previous version of a post if you don't like the current edit. However, the drawback of this feature is that it is not always needed and it fills the post table full of stuff you will never need. Fortunately, turning this feature off isn't too much of a pain. All you need to do is add the following line of code to your wp-config file, just below the DB_COLLATE line.

define('WP_POST_REVISIONS', 0);

You can also set the autosave interval here to something greater than the default of 60 seconds. It is possible to do this in the wp-config file since version 2.5.0.

define('AUTOSAVE_INTERVAL', 123);

If you want to get rid off all of the post revisions from your post table then you can use the following SQL query.

Find File Extension In PHP

This simple code example uses a combination of strrchr to find the last occurrence of a string and substr to return part of the string in order to find the file extension for a given filename. This is ideal if you want to quickly find a file extension.

$ext = substr(strrchr($fileName, '.'), 1);

This code can be used in the following way.

$fileName = '\path\to\file\afile.wibble';
$ext = substr(strrchr($fileName, '.'), 1);
echo $ext;

The output here is 'wibble';

Adding A Sortable List To Wordpress

A sortable list is simply a list of items that can be dragged and dropped to alter the order of those elements.

There are two sortable lists available in Wordpress, one from the JQuery framework and one from the Scriptaculous framework. For a sortable list you will need a list, so here is a simple one.

<ul id="sortcontainer">
	<li class="sortable">Item 1</li>
	<li class="sortable">Item 2</li>
	<li class="sortable">Item 3</li>
	<li class="sortable">Item 4</li>
</ul>

JQuery Sortable

Write To The Output Buffer In PHP

The first thing you learn about in PHP is probably how to print something. This is usually done with a call to the echo or print, but there is another way to print things by writing content directly to the output buffer. The following code looks like you are writing to a file, but the text will appear in the browser window because we are writing to the php://output output stream.

$fp = fopen("php://output", 'r+');
fputs($fp, "Hello World");

Or another way...

file_put_contents("php://output", "Hello World");

The php://output stream is an encapsulation between PHP and the browser. The stream doesn't really exist, but PHP knows what to do with it.

Things To Know About If Statements In PHP

Going back to basics can be useful, even for the most experienced developers. Different languages act and respond in different ways and knowing exactly how some control structures work can be very useful in the long run.

I remember when coding in VB (some years ago now) and realizing that an if statement didn't work like I had expected it to work. I had expected that as soon as a condition was met the code would execute whatever was in that condition and drop out of the if statement. What was actually happening was that the code for any condition met was being executed. I'm not sure if VB still works like that, but it made me realize that a developer must know these things or they could write a system crippling bug without even realizing it.

PHP if statements have been written with efficiency in mind. If a condition is met then the code in the condition is run and the rest of the if statement is skipped. Take the following simple example:

Using Redirection Inside a Plugin In Zend Framework

I had a situation the other day where I had an application in Zend Framework and I wanted to redirect a user to another page. This is fine if you are inside a controller as you can use the _redirect() controller helper, but in this instance I was running the code from inside a plugin and so therefore didn't have direct access to the controller.

The solution is to use the getResponse() method, which is accessible to plugins, and which will retrieve the response object. The response object has a function called setRedirect() that is used to redirect. Any headers that have been issued will be overwritten by this function. The following code can be run inside your zend framework plugins to redirect the user to a different page.