11/10/2015 - PHP
Say for example you have an integer value but you don't want people to see it as is. What you can do is, you can convert it to unique code then it can be decoded again later. For that we can use base_convert() function.
Value for $suffix
can be anything you want.
class BaseConverter
{
private $suffix;
public function __construct($suffix)
{
$this->suffix = $suffix;
}
public function convert($value)
{
if (is_int($this->suffix)) {
$value = strtoupper(base_convert($value.$this->suffix, 10, 36));
} else {
$value = strtoupper(base_convert($value, 10, 36));
}
return $value;
}
public function inverse($value)
{
if (is_int($this->suffix)) {
$value = substr(base_convert($value, 36, 10), 0, -(strlen($this->suffix)));
} else {
$value = base_convert($value, 36, 10);
}
return $value;
}
}
$value = 123456;
$bc = new BaseConverter(1907);
$encoded = $bc->convert($value);
$decoded = $bc->inverse($encoded);
echo "ORIGINAL: $value | ENCODED: $encoded | DECODED: $decoded";
ORIGINAL: 123456 | ENCODED: KF0Y2B | DECODED: 123456
It may lose precision on large numbers so be careful when using it. Try with 1234567890987654321
.