PHP-URL-Shortener
PHP-URL-Shortener copied to clipboard
String offset cast occurred
I'm currently running PHP 5.4.16 and get the following error when i use the getShortenedURLFromID function:
Severity: Notice Message: String offset cast occurred
Line: $out = $base[fmod($integer, $length)] . $out;
Solution:
Convert $base into an array with the following format:
$base = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => 'a', '11' => 'b' etc.
If i find time i'll open an pull request
You could use str_split():
function getShortenedURLFromID ($integer, $base = ALLOWED_CHARS)
{
$length = strlen($base);
$base = str_split($base);
$out = '';
while($integer > $length - 1)
{
$out = $base[fmod($integer, $length)] . $out;
$integer = floor( $integer / $length );
}
return $base[$integer] . $out;
}
Thanks, @osthafen !
@osthafen can u help me for this issue? im use PHP: 5.6.21
Thanks @osthafen, was cracking my head with the same problem.
@RationalEar how to fix code sir?
@kudataz, in the getShortenedURLFromID() function, just convert the string variable $base to an array by adding this line before the while statement:
$base = str_split($base);
BEWARE SCAM ALERT!