Base64 encode and decode string in PHP

Base64 is a group of similar encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. Base64 encoding schemes are commonly used when there is a need to encode binary data that need to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.
more on wiki..
 

function base64encode($text) {
	return strtr(base64_encode($text), '+/=', '-_,');
}
 
function base64decode($text) {
	return base64_decode(strtr($text, '-_,', '+/='));
}

Usage:

echo 'base64:<b>'.base64encode('http://webnetsoft.gr').'</b><br>';
echo 'plain:<b>'.base64decode('aHR0cDovL3dlYm5ldHNvZnQuZ3I,').'</b>';

Output:
base64:aHR0cDovL3dlYm5ldHNvZnQuZ3I,
plain:http://webnetsoft.gr

Categories: PHP Tags: , , , ,
%d bloggers like this: