Work Out Real File Sizes With PHP

6th June 2008

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

5th June 2008

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.

Get Percentage Of A Number With PHP

4th June 2008

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.

Turn Off Analytics In Preview Mode With Wordpress

3rd June 2008

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.

Remove Blank Entries From An Array With PHP

2nd June 2008

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.

Getting An Excerpt Of Text With PHP

1st June 2008

I have talked before about truncating strings to include whole words, but what if you want to have complete sentences? The following function will truncate a string the number of words specified and until the sentence ends.

Work Out The Number Of Days Before A Date

31st May 2008

Use the following function to work out the number of days between today and a date in the future. The function takes three parameters of the day, month and year of the date in question.

 

To use this function just give it a date, here are some examples.

Controlling Error Reporting On Different Servers With PHP

30th May 2008

Having a testing server is quite a common practice, but what happens when you want to go live and you don't want all of your error messages displayed?

A good way of doing this is to create a variable that you can then use to detect what server your code is running on and set your error displaying accordingly. The following section of code will set a variable to true if the code is not running on the server name www.hashbangcode.com. This variable is then used to set the error reporting level.

Transform Text Links Into Anchors

28th May 2008

Using the strip_tags() function on any user generated input should be common practice in order to stop users putting in odd bits of HTML and messing up your nicely coded HTML. You might want to allow anchor tags to be used, which is fine, but some users might use this to include onmouseover and other events in order to run JavaScript annoyances on the page.

So after stripping all tags from your text you can then run the following function to turn any links into anchor elements. This will work with any plain text that you want to use it on.

JavaScript To Stop Frames

27th May 2008

The following section of JavaScript will detect if anyone is viewing your page in a frame. If they are then the code will redirect them to your site. It essentially stops people using frames to poach your content and stops online proxy software from viewing your site.