PHP Logo

Print A Current Copyright Notice With PHP

17th June 2008 - 3 minutes read time

One thing that is inexcusable on any website is printing a copyright notice that is out of date. There are many protagonists of this crime and it is fairly easy to spot.

However, rather than go through all of the pages on your site and hand code in the copyright notice you could just put in the following little bit of PHP.


Copyright <?php echo date("Y"); ?> #! code

This will print off a current copyright notice, no matter what the year is.


Copyright 2008 #! code

The date() function is built into PHP and takes two parameters. The first (required) parameter is a format string for the date. In this example we are giving a single capital "Y", which returns a numeric representation of the year with 4 digits.

Read the full article

PHP Logo

Calculate Distance Between Two Geographical Points With PHP

16th June 2008 - 2 minutes read time

Use the following function to work out the distance between two geographical points. Geographical points are usually longitude and latitude, in degrees. The first thing to do is to convert these values into radians (using the deg2rad() PHP function) so that we can work with them. The four basic parameters used are the longitude and latitude values for the two points. The optional fifth value is to have the end value returned in miles, rather than kilometres.

Read the full article

PHP Logo

Shortening Long URLs With PHP

13th June 2008 - 2 minutes read time

Print out a full URL for a link will sometimes mess up your formatting, especially if you URL is quite long. This might be the case if you are linking to a Google search page, or have an automated script that shows numerous URLs of indeterminate length. The following function will reduce any URL longer than 45 characters by splitting it in two and join them up with a simple string.


function shortenurl($url)
{
 if ( strlen($url) &gt; 45) {
  return substr($url, 0, 30)."[...]".substr($url, -15);
 } else {
  return $url;
 }
}

You can use the function in the following way.

Read the full article

PHP Logo

Preparing A URL With PHP

12th June 2008 - 4 minutes read time

There might be many instances where you will create a program in PHP that takes a URL as input and does something with the address. This might be a site analysis or an image resize, but whatever the use is, you need to be sure that the URL will work or at least has the same format.

What users tend to leave out of a URL string is the http:// bit at the start. You could validate the URL to force the user to do this, but you will end up annoying a few people. By far the best way of making sure that the URL has the http:// bit at the start is by adding it behind the scenes. The best way to this is to remove the http:// from the start of the string, even if it isn't there and then add it back on.

Read the full article

MySQL Logo

Get Information About A MySQL Database With PHP

11th June 2008 - 4 minutes read time

By using the MySQL command.


SHOW TABLE STATUS FROM database;

You can get all sorts of information about a database. The query returns each table as a row and gives lots of information about each table. Using this query it is possible to work out some usage data for the database as a whole. The following function will take a database name and a database resource handle and return how big that database is, the number of tables, and the number of rows in those tables. You will probably have a maximum limit to the amount of data that you can store in your database, so this function is useful to make sure that you don't exceed this limit. This function also uses another function found on the #! code site called readableFileSize() to give more meaningful data sizes.

Read the full article

PHP Logo

Find The Nearest Word Using The Levenshtein Function In PHP

10th June 2008 - 6 minutes read time

The levenshtein() function is part of a set of functions that are used to look at the structure of a string depending on how the string sounds, using levenshtein() allows you to look at the total difference between two strings, defined as a distance value. The important feature of this is that you can compare one string to another and see if they are similar.

The levenshtein() function takes two parameters, these are the two strings that you want to compare with each other. If the two strings are the same then the distance is zero, the higher this value is the more distance there is between two strings. Here are some examples.

Read the full article

PHP Logo

Sorting A Multidimensional Array With PHP

9th June 2008 - 5 minutes read time

Sorting an array is easy in PHP thanks for the built in sorting functions. However, when it comes to sorting a multidimensional array you need to employ these functions in a certain way, especially if you want to vary the data item you want to sort by.

Take the following defined array, taken from the top news stories in the Science and Nature section of the BBC website.

Read the full article

PHP Logo

Work Out Real File Sizes With PHP

6th June 2008 - 3 minutes read time

Displaying the size of a file in bytes is all well and good, but means little to most people so converting the size into something a little more readable is a must. The following function will take the number of bytes that a file is and convert it into something a little more meaningful. Rather than just convert the value into kilobytes it returns the largest denomination of size. So if your file is greater than 1073741824 bytes the function will return your value in gigabytes.

Read the full article

PHP Logo

Check Backlinks With PHP

5th June 2008 - 4 minutes read time

Backlinks are an important part of search engine optimisation and are also useful in seeing what sort of things are popular on your site. If you have a list of known backlinks that you want to keep track of then you can do it manually, or you can get a script to do it for you.

The following function takes two arguments, the first being the remote URL and the second is the URL you are checking for. The function works by doing a small amount of initial formatting on the URL you are checking for and then downloading the remote page in little bits and seeing if each bit contains a link. If it does then the function breaks out and returns true. If the link isn't there then it returns false.

Read the full article

PHP Logo

Get Percentage Of A Number With PHP

4th June 2008 - 2 minutes read time

Use the following function to find the percentage value of one number to another. If the $total parameter is zero then zero is returned as this would otherwise result in a division by zero error.

function get_percentage($total, $number)
{
  if ( $total > 0 ) {
   return round(($number * 100) / $total, 2);
  } else {
    return 0;
  }
}

Here are some examples of the function in action.

echo get_percentage(100,50).'%'; // 50%
echo get_percentage(100,10).'%'; // 10%
echo get_percentage(100,100).'%'; // 100%
echo get_percentage(400,3).'%'; // 0.75%
echo get_percentage(1234,4321).'%'; // 350.16%

Read the full article