PHP

Posts about the server side scripting language PHP

Backup MySQL Database PHP Script

There are quite a few scripts available on the Internet that allow you to dump data from a database into a format that can be used to replicate that database structure elsewhere. The following function is my take on this commonly occurring script.

Getting Started With Zend Tool

The latest versions of Zend Framework come with a handy little tool that gets you up and running with a basic Zend Framework install within a few moments. When you have downloaded Zend Framework you will notice that in minimal and full there is a directory called bin. This seems to be intended to contain lots of different tools, but at the moment it contains three files that are part of Zend Tool.

The important file in this directory is zf.php which is the actual core of the tool. The other two files are used depending on what operating system you are running. If you are running Windows then it is zf.bat that will be used, otherwise it is zf.sh that is important.

Extract Keywords From A Text String With PHP

A common issue I have come across in the past is that I have a CMS system, or an old copy of Wordpress, and I need to create a set of keywords to be used in the meta keywords field. To solve this I put together a simple function that runs through a string and picks out the most commonly used words in that list as an array. This is currently set to be 10, but you can change that quite easily.

The first thing the function defines is a list of "stop" words. This is a list of words that occur quite a bit in English text and would therefore interfere with the outcome of the function. The function also uses a variant of the slug function to remove any odd characters that might be in the text.

PHP Class To Interact With is.gd API

The is.gd URL shortening service is yet another site that converts a long URL into a small one, but is.gd differs in that it generally produces smaller URLs than others. The is.gd API doesn't require that you create an account on the site, but there are one or two limitations to using the service that you should be aware of first. You can find out these limitations by reading the is.gd terms and conditions. You can use this class to interact with is.gd, but the service doesn't have a massive amount of features the class only contains shorten() and expand() functions that work exactly as expected. You can download the class using the following link:

Download the is.gd API interaction PHP class.

Lazy Instantiation In PHP

Lazy instantiation (also known as lazy load) is an object orientated design pattern that attempts to reduce the amount of resources needed to load an application by only loading certain parts of it if they are needed. This makes sense as you don't need all parts of an application on every page load, so cutting down the data loaded cuts down the resources and processing time needed to load the page.

A good example of this might be when creating a student and courses application or similar. Obviously you would want to link students with their courses, but you don't want to have to load all of the students registered with a course when you are looking at a list of courses. Lazy instantiation allows you to only load the student information when it is called for.

To build this example we will first need a Student class.

PHP Class To Interact With bit.ly API

There are a few URL shortening services about, of which bit.ly is just one. However, it does provide a nice API for developers to interact with the site. Not only is it possible to shorten URL's, you can also reverse them, find out information about the site and get information about how many people have used to link. The API requires you to create an account to interact with the API. Once you have created an account you can use the API service.

Download the bitly API interaction PHP class.

There are several functions available in this class but the main two are shorten() to turn a URL into its bit.ly equivalent and expand(), which will do the exact opposite.

To convert a URL into its bit.ly equivalent you can call the shorten() function like this.

PHP Variable Assignment Within If Statement

The usual practice when checking for the return value of functions is to run the function and store the value in a variable, and then test that variable. Here is an example of that process using the strstr() function.

$string = 'abcde';
$var = strstr($string, 'f');
if ( false !==  $var ) {
    var_dump($var);
} else {
    var_dump($var);
}

This code will output "bool(false)" as that was the return value of the strstr() function.

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.

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.