PHP

Shortening Long URLs With PHP

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) > 45) {
  return substr($url, 0, 30)."[...]".substr($url, -15);
 } else {
  return $url;
 }
}

You can use the function in the following way.

Preparing A URL With PHP

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.

Get Information About A MySQL Database With PHP

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.

Find The Nearest Word Using The Levenshtein Function In PHP

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.

Sorting A Multidimensional Array With PHP

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.

Work Out Real File Sizes With PHP

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.

Check Backlinks With PHP

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.

Get Percentage Of A Number With PHP

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%

Turn Off Analytics In Preview Mode With Wordpress

The preview mode in Wordpress exists to allow you to see how a post will look before you publish it to your site. This is an important feature if you have lots of readers as once your click that publish button your post will be live and your RSS feed will be pinged out to lots of different services. Previewing a post can avoid embarrassing spelling or formatting errors before the post goes live.

Preview uses the same templates files as your blog and so will load all of the same files as if you normally view your site. The only small issue with this mechanism is that any analytics code you might have installed will be run as well and you might see that you most visited pages are you previewing posts before putting them live.

Of course you could just filter out your IP address in your analytics, but if your Wordpress blog has lots of different authors writing content from many different locations it is unfeasible to continuously edit your analytics settings.

Remove Blank Entries From An Array With PHP

If you have an array that you want to remove any null data items from then you can use the following function. It will create a new array and only copy across items from the existing array if they contain a value. If the value is an array the function calls itself and makes sure that the returned array contains something before adding it to the new array.

function array_purge_empty($arr) {
 $newarr = array();
 foreach ($arr as $key => $val) {
  if (is_array($val)) {
   $val = array_purge_empty($val);
   // does the result array contain anything?
   if (count($val) != 0) {
    $newarr[$key] = $val;
   }
  } else {
   if (trim($val) != "") {
    $newarr[$key] = $val;
   }
  }
 }
 return $newarr;
}

Here is an example of the function in action.