Articles

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';

Getting Started With Zend_Lucene

Zend_Lucene is an implementation of the Lucene search engine in PHP5 and is included as part of the Zend Framework from version 1.6. Lucene implements all of the standard search engine query syntaxes (eg. boolean and wildcard searches) and stores its index as files so it doesn't need a database server to run. Lucene can be used if you want to add search functionality to a site but don't want to go down the route of building a querying syntax from scratch.

To get started with Lucene you need to create an index. The following code has the effect of creating a directory on your server that Lucene will use to store and retrieve documents.

$index = Zend_Search_Lucene::create('/data/my-index');

To open the index use the following code.

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

7 Tips On Writing Your CV

Working for a number of years in the web industry means that I get to see quite a few CVs, and after a while you get a feel for what makes a good one. Your CV (or resumé) is quite often the very first thing anyone ever hears about you, so it is important to get it right. To that end I thought I would impart some of the things I have learned after years of writing my own and reading other people's CVs.

Wordpress Post Friendly Code With JavaScript Replace

I recently talked about adding code to blogs and comments to Wordpress and making sure that certain characters are encoded properly. So to simplify things I thought I would create a little set of regular expressions that takes a sample of code and convert it into a Wordress friendly format. It consists of the following function, which takes the value of a text area called tochange and runs some regular expression replace functions on it. I have kept the expressions as simple as possible so they are quite easy to understand. The g argument for each expression means that the replace will be done for all of the text.

Irritating JavaScript Virus Message Popup And Redirect

The other day I was approached by a friend who had this odd looking virus message on their screen. They said that they hadn't been doing anything in particular, just writing an email and surfing the net in Firefox when all of a sudden this pop-up appeared on screen and told them they had a virus.

The thing that caught my friend by surprise was that they were using Firefox and therefore shouldn't be able to get pop-ups. However, on closer inspection is appeared to be a JavaScript confirm message. My friend clicked on the Cancel button and one of the pages they had open redirected to this very dodgy looking site which promptly started to do a dummy virus scan.

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: