PHP-URL-Shortener icon indicating copy to clipboard operation
PHP-URL-Shortener copied to clipboard

String offset cast occurred

Open SnakeME opened this issue 11 years ago • 8 comments

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;

SnakeME avatar Jul 25 '13 12:07 SnakeME

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

SnakeME avatar Jul 25 '13 13:07 SnakeME

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;
}

osthafen avatar Aug 19 '13 12:08 osthafen

Thanks, @osthafen !

ghost avatar Jun 10 '15 04:06 ghost

@osthafen can u help me for this issue? im use PHP: 5.6.21

kudataz avatar May 29 '16 20:05 kudataz

Thanks @osthafen, was cracking my head with the same problem.

RationalEar avatar Jun 02 '16 08:06 RationalEar

@RationalEar how to fix code sir?

kudataz avatar Jun 18 '16 22:06 kudataz

@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);

RationalEar avatar Jun 20 '16 06:06 RationalEar

BEWARE SCAM ALERT!

kokomoecode avatar Feb 20 '24 18:02 kokomoecode