Articles

W3C Validation PHP Class

If you want to incorporate a W3C validation check into an application then you can use the following class. It uses file_get_contents() to get the contents of the file and then uses regular expressions to return either the number of errors or -1 is any errors occur. Of course if the document is valid the function will return 0.

PHP Function To Get TinyURL

TinyURL is a service where you can convert a long URL string to a really small one. For instance, the following URL, which points to the Googleplex on Google Maps.

http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=googleplex&sll=37.579413,-95.712891&sspn=34.512672,45.615234&ie=UTF8&cd=1&z=16

Can be converted to the following.

http://tinyurl.com/qpkor2

This is perfect for posting to Twitter or similar microblogging platforms as it saves lots of character space.

Add A Post To Twitter Button To Your Wordpress Posts

This is quite a simple thing to do and it is quite easy to add a little link to your Wordpress posts. If the user is logged into Twitter on the web (which isn't all that common with multiple Twitter apps taking over) then when they click on a link they will be taken to their Twitter profile and their status box will contain the text you have set. You won't actually be able to update a users status for them, they have the final control over that, but some users will update their status so it is worth doing.

This is all done through a URL using the status parameter, like this.

http://twitter.com/home/?status=text+to+post

Because this is a URL you will need to encode it properly in order for Twitter to understand it. Using Wordpress you need to put a call to the urlencode() function to encode any string you might want to post.

Using PHP implode() To Construct Strings

If you are constructing a simple string from a set of variables contained in an array then you can use the implode function to convert the array into a string. The implode() function takes two parameters. The first is the glue that is used to join the items in array together and the second is the array to use. Here is a trivial example of implode() in action.

$array = array(1, 2, 3, 4, 5, 6);
 
echo implode(',', $array);

This will print out the following:

1,2,3,4,5,6

The good thing about the implode() function is that it doesn't add stray commas to the start and end of the string so there is no need to alter the string after the function is used.

Starting And Stopping Apache Using Windows .bat Files

Following on from the post about starting and stopping MySQL using .bat files I decided to add commands to these files that also controlled Apache in the same way. This turned out to be a lot easier than trying to start and stop MySQL as the command line commands for httpd executable worked very well in .bat files.

So, to start Apache use the following line.

"C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin\httpd" -k start

And to stop the server use the following line.

"C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin\httpd" -k stop

Pixelate Images With PHP

The other day I needed to hide an image but keep the original image intact so rather than do complicated things with light and dark filters I decided the best way would be to pixelate the image. All that is required to pixelate an image is to shrink it down and then blow it up to the original size, thus reducing the quality of the image.

The following code will do this process for a jpeg image. The pixelate amount variable is used to reduce the width and height of the first image using division and then multiply them back again to the original values for the output image. Remember to change the ImageCreateFromJPEG() to the format you want.

A Look At robots.txt Files

A robots.txt file is a simple, static, file that you can add to your site in order to stop search engines from crawling the content of certain pages or directories. You can even prevent certain user agents from crawling certain areas of you site.

Lets take a real-world example and look at what you would do if you decided to set up a Feedburner feed in place of your normal RSS feed. I won't go into why you would do this much, other than to say that you get some nice usage statistics and it can save on some processing power on your server as your feed is only looked at when Feedburner updates. Once you have allowed your blog to issue the Feeburner feed instead of your normal feed you then need to stop search engines from indexing the old feed. This stops is appearing in search indexes and things so that you can get your users to grab the Feedburner feed and not your local feed. You would then put a robots.txt file in place with the following content.

Starting And Stopping MySQL Using Windows .bat Files

I use my PC for a lot of different things, and I don't necessarily need to have MySQL server running all the time, especially when I want to play a game. So I wondered if there was a simple way in which I could start and stop the server using a simple .bat file.

I had a look at the MySQL website and found a page that details how to start MySQL from the command line. This page suggested that I use the following command.

"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld"

Use the following command to stop the server. In this case the username is root and the password is wibble.

Excel Document Scanning With Zend_Search_Lucene

Zend_Search_Lucene offers some powerful document scanning capabilities, and there are a few different formats that are useful for the search engine to index.

To allow the indexing and searching of Excel documents using Zend_Search_Lucene you need to use the Zend_Search_Lucene_Document_Xlsx class. However, to use this class you must have the Zip module installed with PHP. For Windows users this means editing your php.ini file and uncommenting the following line:

extension=php_zip.dll

For Linux users you will need to recompile PHP with the --enable-zip configure option.

Create and/or open the index in the normal way and you can index Excel documents using the following code.

Multi Page Forms In PHP

Multi pages forms are just as they sound, a single form spread across multiple pages. These are useful in terms of usability as it can break up an otherwise dauntingly big form into smaller chunks. It can also be useful if you want to process some of the results in order to determine what forms the user sees on later steps.

There are two ways in which it is possible to do this using PHP.

The first (and simplest) is just to cycle through the items submitted on a previous form and print them out as hidden fields. Our first page source code will look like this: