Skip navigation.

Harold's Home

   Home > PHP > CLI Fun
  
PHP Scripts

Index
CLI fun
Mail on 404
HB-NS (NewsScript)

Downloads

Applescripts
APOD to Desktop
Dreamweaver Extensions

Stuff

Writings
Other stuff
Central Grinder

OOOk Default:

VJ stuff
VJ Tools
Bananas
Strippers
Sample Movies

urldecode or urlencode from the commandline

This page describes the PHP commandline script to urlencode or -decode a string.
I also have an online version available for your enjoyment.

The following script needs a functional CLI version of PHP, it is only tested on OS X. For other platforms: try the php.net website and let me know if the script works as expected.

This page contains a small script that is fairly well commented that either urlencodes or urldecodes a string. Ever looked at the adressbar in your browser and wondered what some of the cryptic things in the following url mean: http%3A%2F%2Fwww.haroldbakker.com%2F? This script will help you find out.

The code

#!/usr/local/php/bin/php
< ?php
//
// this script expects to be called from the commandline
// it expects to be called with an action to perform and then a string to translate
// you can either enter the full name of the action or type the Capital letter (recommended)
// if the string to translate contains spaces put single straight quote (') marks around it
// example: ./urldecode_or_urlencode.php E 'http://www.haroldbakker.com/ is a cool site'
// example: ./urldecode_or_urlencode.php D 'http%3A%2F%2Fwww.haroldbakker.com%2F+is+a+cool+site'
//
$myaction = $argv[1];
// translate input to lowercase
// so it doesn't really matter if someone enters an E or an e or even URlEnCodE etc.
$myaction = strtolower($myaction);
$mystr = $argv[2];
if (($myaction == "d") || ($myaction == "urldecode"))
{
echo urldecode($mystr) . "\n";
}
else if (($myaction == "e") || ($myaction == "urlencode"))
{
echo urlencode($mystr) . "\n";
}
// if no choice is made or a nonsensical one ask again
else
{
echo "usage: $argv[0] D|E 'string to translate'\n";
echo "\tuse D for urldecode, E for urlencode\n";
}
// visit http://www.haroldbakker.com/ if you liked this script
// the script ends here
?>

Download the code: urldecode_or_urlencode.php.sit

How to use

Expand the sit archive and put the resulting file in the directory ~/bin in your homefolder (create this if it doesn't exist). Then open Terminal.app (in /Applications/Utilities) and enter
cd bin; chmod 755 urldecode_or_urlencode.php
Your script is now ready to go. Type ./urldecode_or_urlencode.php to use the script.
As is documented in the code the script expects to be called with an action to perform and then a string to translate. Chances are you will want to urlDecode a string like the following gobbledygook: http%3A%2F%2Fwww.virtualpetrock.nl%2
Fstatisticsdetails2.php%3Fcn%3D1040054091%7CMr.%2BFluffy
.
You can simply enter a D or an E, depending on what you want to do.
After that the terminal will ask you for the text to be translated.
Enter that and the script will spit the translated code back and quit.

Easy as pie:
Terminal session described above

The script will only echo the translated string, which could be useful if you want to invoke the script from other scripts or pipe the output to something like bbedit:
./urldecode_or_urlencode.php E 'http://www.haroldbakker.com/' | bbedit.

XML version of this site
About, copyright, privacy and accessibility | Mail