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.
// 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;
// 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;
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
echo "usage: $argv 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:
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:
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.