PHP

Validate EAN13 Barcodes

EAN13 barcodes are commonly used to label products in Europe. If you want to know more about how they work then please view the Wikipedia entry on European Article Numbers.

EAN13 barcodes are actually 12 digits long and are validated by using a check digit, which is placed at the end, making the code 13 digits long. The check digit is worked out by the following process:

Create A Tag Cloud Page In Wordpress

A tag cloud is a name for a collection of keywords that are displayed as a big block of text. Usually the most commonly occurring keyword is the largest, and the least commonly occurring keyword is the smallest. Tag clouds are a neat way of allowing your users to navigate your content in a different way, simply be letting them look over the cloud and linking each keyword to sections of your site that contain that word.

Wordpress comes with this function built in and is already available as a widget. However, after adding a few hundred posts this tag cloud can get rather large and this widget can stretch your sidebar to stupid proportions. A way around this is to create a page that can be used to display this large tag cloud. The first step is to create a page template that will be used to run the correct Wordpress function to display the tags. This is done by making a copy of your normal page template (call it tagpage.php) and adding the following code at the top.

Hide And Unhide Code With PHP

If you are selling a system the last thing you want is for people to copy the system and pass it on for free. There are numerous ways to implement parts of the system that will stop this from happening.

By far the easiest is to create a section of code that is hidden, the removal of which will cause the application to fall over. It could even be as simple as a link back to your site so that even if you give you application away for free, you will always have that link present.

This method involves the use of a function called eval(), which takes PHP code as a string and interprets it to produce output. Here is an example that prints a link to #! code.

Print Array Without Trailing Commas In PHP

I have previously talked about Removing commas from the end of strings, but it is also possible to use the implode() function to do the same sort of thing.

implode() takes two parameters, the separator and the array, and returns a string with each array item separated with the separator. The following example shows how this function works.

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

The $list variable will now contain the string "1,2,3,4,5,6". However, things tend to become messy again when you have an array with empty items in it.

PHP Array Of Australian States

Use the following array to print out a list of Australian states.

$australian_states = array(
    "NSW" => "New South Wales",
    "VIC" => "Victoria",
    "QLD" => "Queensland",
    "TAS" => "Tasmania",
    "SA" => "South Australia",
    "WA" => "Western Australia",
    "NT" => "Northern Territory",
    "ACT" => "Australian Capital Territory"
);

 

PHP Array Of USA States

Use the following array if you want to print out a list of USA states either as a list, or as a select box.

Reading And Writing To Compressed gzip Files

Reading and writing to compressed gzip files can be done in much the same way as reading and writing normal files. The main difference is the use of some special functions that compress and uncompress the data. Rather then use fopen() to open a file, you open a compressed file with gzopen(). This is the case for many of the file access functions, although you should be aware that they don't all work exactly the same as each other.

Ping A Website Using PEAR's Net_Ping

Net_Ping is a PHP wrapper for the ping program and provides a neat way pinging a host or checking if a site is up and running. To install it you just need to have pear installed. Once that is done you can do a search for PEAR Net_Ping by opening a terminal and typing:

> ping search ping

This will return the following:

Retrieving data...0%.MATCHED PACKAGES, CHANNEL PEAR.PHP.NET:
=======================================
PACKAGE           STABLE/(LATEST) LOCAL
Net_Ping          2.4.4 (stable)        Execute ping
Services_Pingback 0.2.2 (alpha)         A Pingback User-Agent class.

You can install Net_Ping by running the following:

> pear install Net_Ping

Which should produce the following output.

Redirect From HTTPS To HTTP Using PHP

If you have a site with parts of it using SSL, but want to turn it off for mundane pages like the blog section then use the following code. This uses the $_SERVER['HTTPS'] variable to see if HTTPS is turned on, if it is then a header is issued and the page redirected.

if ($_SERVER['HTTPS'] == 'on') {
    $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('Location: ' . $url, true, 301);
    exit();
}

You can turn it back on again on your secure pages using the opposite.

if ($_SERVER['HTTPS'] != 'on') {
    $url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('Location: ' . $url, true, 301);
    exit();
}

Don't include both of these scripts on the same page or you will break the site! Also, make sure that no headers have been issued before hand as this code can cause a "headers already issued" error.

Delete Trailing Commas In PHP

Converting an array of information into a string is easy, but when you are doing this for insertion into a database having trailing commas is going to mess up your SQL statements.

Take the following example, which takes an array of values and converts them into a string of values. This practice is quite common in PHP database manipulation.

$values = array('one', 'two', 'three', 'four', 'five');
$string = '';
 
foreach ( $values as $val ) {
    $string .= '"'.$val.'", ';
}
 
echo $string; // prints "one", "two", "three", "four", "five",

Obviously we need to strip the trailing comma from the end of this string. To do this you can use the following function.