You cannot have something represented in Base64 with just this character range (A-Z, a-z and 0-9) as this range has only 62 characters and Base64 requires 64 distinct representations.
So if you don’t want the characters + and / in its representation in Base64, you will need to replace them with something else outside this range. You will have to choose a replacement for the = also because it can appear in a representation in Base64 in order to complete the size of the last block.
What has been used in practice, when the need for example to include a Base64 representation in a URL, is to replace the set {+ / =} for {- _ ,}, respectively.
In a quick search, it seemed to me that PHP doesn’t natively have a function for this, so you’ll have to implement your own.
Even if your intention is not to use in URL, this idea should suit you:
function base64url_encode($plainText) {
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/=', '-_,');
return $base64url;
}
function base64url_decode($plainText) {
$base64url = strtr($plainText, '-_,', '+/=');
$base64 = base64_decode($base64url);
return $base64;
}
Update: It just occurred to me too that you can convert your bytes to Hexadecimal, which is represented only by 0-9 and A-F. The resulting string is much larger than in the Base64 representation, but it may suit you. I don’t know PHP function that does this but the logic of converting bytes to hexadecimal is quite simple.
https://github.com/vinkla/base62
– gmsantos
@gmsantos I appreciate the reference but I already knew, but as I explained to me the Base62 coding of numerical values is basic and linear. What I want is an encoder with the range (A-Z, a-z and 0-9) but for strings. And that I don’t find and I think I really have to develop one and nail it. But.
– chambelix
@chambelix saw my answer?
– Victor Stafusa
@Victor saw and I’ll answer I’m analyzing :)
– chambelix