I’m using this guide – http://kevin.vanzonneveld.net/techblog/article/create_short_ids_with_php_like_youtube_or_tinyurl/
to generate short codes for URL.
But bcpow() isn’t working anyhow on my system.
I’m using php-cli and phpinfo(); shows bcmath is installed.
Numeric to shortcode –
function aplhaIdCalc( $in ) {
$index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = 62;
$out = "";
for ($t = floor(log($in, $base)); $t >= 0; $t--) {
$bcp = bcpow($base, $t);
$a = floor($in / $bcp) % $base;
$out = $out . substr($index, $a, 1);
$in = $in - ($a * $bcp);
}
$out = strrev($out); // reverse
return $out;
}
Shortcode to numeric –
function idAlphaCalc( $in ) {
$index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = 62;
$in = strrev($in);
$out = 0;
$len = strlen($in) - 1;
for ($t = 0; $t <= $len; $t++) {
$bcpow = bcpow($base, $len - $t);
$out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
}
$out = sprintf('%F', $out);
$out = substr($out, 0, strpos($out, '.'));
return $out;
}
How can I use these functions without bcpow and yet get similar output and input?
I can’t understand these BCMath functions but I think base_convert might work.
Edit : Changing bcpow to pow works. What are the risks involved in using pow ?
Diference between Math-Functions and BC Math-Functions:
handle values within the range of the
integer and float types on your
computer
mathematics PHP offers the Binary
Calculator which supports numbers of
any size and precision, represented
as strings.
So precision is the answer.
Sure
bcmathis loaded? Testet your functions on my local environment. no problem at all.And you got a typo in your first function
aplhaIdCalc=>alphaIdCalc