Home  >  Article  >  Backend Development  >  生成短链接,php5.6可用,为什么php7生成不了?

生成短链接,php5.6可用,为什么php7生成不了?

WBOY
WBOYOriginal
2016-06-06 20:15:53920browse

function shortUrl($long_url){
    $key = '123';
    $base32 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // 利用md5算法方式生成hash值
    $hex = hash('md5', $long_url.$key);
    $hexLen = strlen($hex);
    $subHexLen = $hexLen / 8;

    $output = array();
    for($i=0;$i> 5;
        }
        $output[$i] = $out;
    }
    // 生成位数
    return $output;

}


print_r( shortUrl('http://www.google.com/') );

php5.6输出结果为:

Array
(
    [0] => MVvIZz
    [1] => qURRjy
    [2] => U7rIzu
    [3] => JNNJbi
)

php7输出结果为:

Array
(
    [0] => aaaaaa
    [1] => aaaaaa
    [2] => aaaaaa
    [3] => aaaaaa
)

这是哪的问题?

回复内容:

function shortUrl($long_url){
    $key = '123';
    $base32 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // 利用md5算法方式生成hash值
    $hex = hash('md5', $long_url.$key);
    $hexLen = strlen($hex);
    $subHexLen = $hexLen / 8;

    $output = array();
    for($i=0;$i> 5;
        }
        $output[$i] = $out;
    }
    // 生成位数
    return $output;

}


print_r( shortUrl('http://www.google.com/') );

php5.6输出结果为:

Array
(
    [0] => MVvIZz
    [1] => qURRjy
    [2] => U7rIzu
    [3] => JNNJbi
)

php7输出结果为:

Array
(
    [0] => aaaaaa
    [1] => aaaaaa
    [2] => aaaaaa
    [3] => aaaaaa
)

这是哪的问题?

问题主要出现在这句话上
$idx = 0x3FFFFFFF & (1 * ('0x' . $subHex));
因为在php7中,十六进制的字符串不再被认为是数字,所以这里所采用的隐式转换变成了无效转换,导致结果出现了问题。
可以将此句替换为
$idx = 0x3FFFFFFF & hexdec($subHex);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn