PHP

Randomising The Middle Of Words In PHP

I was sent an email the other day that contained some text were the start and end letter of each word were left alone, but the middle of each word was randomized. The weird part was that the text was still readable, which is due to the way in which the brain processes words.

I wondered if I could replicate this using a PHP script. All I would need to do is split apart the sentence into the component words and loop through those words, randomizing the middle of them. Clearly, it is not possible to mix up the order of letters in a word less than four characters long so a check would be needed for this. This is what I cam up with:

Generate Password Function In PHP

I have talked about generating random passwords before. Although that function generated some nice passwords, they perhaps aren't as unique as they ought to be.

This function, take from Webtoolkit creates passwords of different length with varying levels of complexity.

Create Images Thumbnails And Cache Them In PHP

Creating image thumbnails is a pretty common practice, and there are a few scripts available that allow you to do this in PHP using the GD2 library. However, they are normally overkill for what should be a simple task, so after a bit of searching and testing I found the following ImageResize class, which is taken from http://shiege.com/scripts/thumbnail/. I have modified the code to be PHP5, but if you want the PHP4 version then you can get it from the site.

Get The IP Address Of A Visitor Through PHP

I have talked previously about getting an IP address of a visitor with PHP. The failing in using the value of $_SERVER["REMOTE_ADDR"] is that if the visitor is using a proxy then you will get the proxy IP address and not the visitors real IP address.

This function works by going through any variables in the $_SERVER array that might exist that would contain information to do with IP addresses. If they are all empty then the function finally looks at $_SERVER["REMOTE_ADDR"] value and returns this as a default.

Moving Files In PHP

I have been asked a couple of times recently if PHP has a function that will move a file, so I thought I would put the solution here.

PHP has a function called rename() which can be used to rename a file, but because of the way it works it can be used to move files. Let's say that you wanted to rename a file called test.txt to test_back.txt, this can be accomplished by doing the following.

rename("test.txt", "test_back.txt");

However, if you want to move the file from one directory to another you can do the following.

rename("test.txt", "backups/test_back.txt");

The return value of rename() is boolean that tells you if the rename was successful or not, this can be used to error check like this.

Recursive chmod Function In PHP

File permissions are important, especially if you want to let a user agent view a file. If the file doesn't have the correct permissions then it will not be accessed and could cause your script to fail. To get around this you might want to use the following function. It uses the PHP function chmod(), which sets permissions, but it does this recursively from wherever you set it off from.

Swap Values Without Temporary Varaibles In PHP

I have talked about swapping values without a temporary third variable before, but there is another way to do this which doesn't make the code unreadable. This is by using the list() function in the following way.

$a = 1;
$b = 2;
list($a,$b) = array($b,$a);

This swaps the two values over. The good thing about this function is that you can swap any number of values over without the need to create lots of confusing temporary variables or using complicated looking bitwise operators. Take 4 variables.

$a = 1;
$b = 2;
$c = 3;
$d = 4;

These values can be entirely swapped using the list() function in the following way.

Quickly Run Any PHP Code Through A Form

NOTE: This bit of code is potentially very dangerous and should NOT be uploaded to your web host. I only use this function on my localhost to quickly check that a snippet of code works.

If you want to quickly run some PHP code, and don't want to have to go through creating a file just to see the outcome of a simple calculation is then this snippet might be of some use to you.

It works by taking the contents of the textarea and creating a file with the filename of 'tempcodefile.php', which contains that code. The created file is then included in order to run the code, the output of which is displayed at the top of the screen.

CodeSniffer File Doc Comment And Class Doc Comment

Yesterday I wrote two posts about CodeSniffer and a common warning that I couldn't find any decent documentation on. I thought that today I would go over something that I also had trouble finding out about, this is the file doc error. When you run CodeSniffer on a class you might find the following error being produced.

PHP Version Not Specified Warning In CodeSniffer

In my last post I talked about the PHP CodeSniffer, so today I thought I would solve a common problem that doesn't seem to have any documentation. Whilst correcting a class file I had there was this one warning that just wouldn't go away.

FILE: myclass.php
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 11 | WARNING | PHP version not specified
--------------------------------------------------------------------------------

There is no documentation (that I could find) on the CodeSniffer site about how to solve this, so I dove into the source files for the CodeSniffer extension and found this line.