Articles

Serialize And Unserialize With PHP

If you have an object or array that you want to save until a later you can use the serialize() and unserialize() functions. The operation of the functions are straightforward. To serialize() an array just pass the serialise function the array like this.

$array = array(1,2,3,4);
$serializedArray = serialize($array);

Now when we print the serialized array out we get the following.

a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}

This contains all of the data needed to recreate our array. Be careful not to edit this string because it will not work if you want to unserialize it and get the array back. You can store this string in a file or a database so that you can recreate the exact same array at a later date.

Use Alt+. To Print Out Last Parameter

A handy trick when using a Unix/Linux system is to repeat the last parameter from the previous line. Lets say that you typed in the following line to move a file to another directory.

$ mv file.txt /usr/local/

To then move into that directory you can just type cd and Alt+. to copy in the last parameter used in the last line. This will put the following on the command line.

$ cd /usr/local/

You can press Alt+. multiple times to go back through your parameter history. Note that it only records the last parameter used for each line. So for the example above, if you pressed Alt+. twice you would get the last parameter of whatever command you executed before moving the file.

The PHP array_flip() Function And Detecting Functions

The array_flip() function in PHP is used to swap the values of an array with the keys. Take the following array.

$array = array('key1'=>'value1', 'key2'=>'value2');

To exchange all the values with the keys we pass it through the array_flip() function.

$array = array_flip($array);
echo '<pre>'.print_r($array, true).'</pre>';

This prints out the following:

Array
(
 [value1] => key1
 [value2] => key2
)

If any of the values are the same then the highest key is overwritten. The following array:

$array = array('a', 'a', 'a', 'b');

Will produce the following array when passed through array_flip().

Print Out A Random Futurama Quote

If you sent a curl request to the slashdot.org server you get back a random Futurama quote contained within the header information. The following curl command:

curl -Is slashdot.org

The commands supplied are I and s. I causes only the header of the file to be shown and s stops curl printing out anything. This returns the following headers:

HTTP/1.1 200 OK
Date: Wed, 25 Jun 2008 08:34:43 GMT
Server: Apache/1.3.41 (Unix) mod_perl/1.31-rc4
SLASH_LOG_DATA: shtml
X-Powered-By: Slash 2.005001
X-Bender: I'm an outdated piece of junk.
Cache-Control: private
Pragma: private
Connection: close
Content-Type: text/html; charset=iso-8859-1

Which contains a quote from Bender. To grab the correct line we then pass this through a regular expression to find a line that starts with an "X" and a dash, followed be either a B (for Bender) or an F (for Fry). The following line:

Bash Fork Bomb And The Cure

A fork bomb is a simple bit of shell code that, once run, will soon fill all available memory and fork space with itself. Here is the code, and remember, don't try this at home!

$ :(){ :|:& };:

To explain what is going on we need to cut this code into sections. The first thing we do is refine a function called ":", which accepts no parameters.

$ :(){};

We then get this function to run itself recursively and also to run another version of itself in the background, this creates another fork of the program.

$ :|:&

Finally we start it all off with the first function call.

HTML And XHTML Doctypes

In order to validate any page of HTML or XHTML you will need a doctype. This is a string of text that sits at the top of the document and tells the browser exactly what markup standard has been used to create the page.

XHTML Strict

This doctype is used in an XHTML document when you are not using any framset or depreciated tags.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

XHTML Transitional

This doctype is used if your XHTML document contains depreciated tags like <b>.<.p>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

XHTML Frameset

Use this XHTML doctype if your document contains either frameset tags or depreciated tags, or both.

Enabling Tabbing In A Textarea

When a user presses the tab key in a browser window the normal action is for the user to move the focus to a different control. To enable a word processor like tab effect in a text area you need to catch the keystroke and add in the tab character to where ever the cursor is. This is the main issue with creating this solution, it is easy to add a tab to the end of the text, but most users might want to add a tab half way through the text.

Take the following HTML text area.

<textarea name="content" class="textarea" id="content" rows="20" cols="65"  wrap="on" onKeyDown="return catchTab(this,event)"></textarea>

When a keystroke is detected it runs the catchTab() function. This function detects if the keystroke is 9 (which means it is a tab) and runs the function called replaceSelection() in order to find out where the cursor is and replace the text that exists there.

Submitting A HTML Form Using JavaScript

In order to submit a form using an event you need to run a click event on any submit button in the form. Take the following form.

<form method="post" onsubmit="alert('Form submitted!'); return false;">
    <input type="text" name="testvalue" value="" />
    <input type="submit" name="submit" id="submit" value="Submit" />
</form>

To run a submit event using JavaScript we just find the submit button via the id and run click() against it.

document.getElementById('submit').click();

That's it!

Time Calculator In PHP

Use the following function to work out how long it has been since an event in years, months, weeks, days, hours, minutes and seconds.

function getAge($year,$month,$day,$hour=0,$minute=0,$second=0){
 $age = mktime($hour,$minute,$second,$month,$day,$year);
 $age = time()-$age;
 return array('years'=>$age/60/60/24/365,
  'months'=>$age/60/60/24/12,
  'weeks'=>$age/60/60/24/7,
  'days'=>$age/60/60/24,
  'hours'=>$age/60/60,
  'minutes'=>$age/60,
  'seconds'=>$age);
}

The practical use of this function is that you can work out how old someone is from their birthday. Here is an example of the function in use.

// someone's birthday
echo '<pre>'.print_r(getAge(1984,10,4),true).'</pre>';

Which would output the following:

Print A Current Copyright Notice With PHP

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.