How to solve Base64 issue with double slashes.

Base64 converts for example the number 1000 to “MTAwMA==”.
It contains the next characters :  “A–Z, a–z, 0–9, +, /, = ” .
Passing Base64 values in Browsers links can be a problem if the encoded value contains double slashes “/”.

This issue will give errors in data validations, because the two slashes will be processed as one, completely changing its value.

The next functions will replace unsafe values “+ and /” with ” – and _ ” witch can be used safely in links and links.

Here is the encode and decode Base64 Url functions :

<?php

$value = "abcd 1000 xyz";

function base64url_encode($s) {
 return str_replace(array('+', '/'), array('-', '_'), base64_encode($s)); 
}
function base64url_decode($s) { 
 return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s)); 
}

$value = base64url_encode($value);

echo $value;
echo "\r\n";
echo base64url_decode($value);

?>

The output :

YWJjZCAxMDAwIHh5eg==
abcd 1000 xyz