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

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

WBOY
Release: 2016-06-06 20:15:53
Original
1026 people have browsed it

<code>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/') );</code>
Copy after login
Copy after login

php5.6输出结果为:

<code>Array
(
    [0] => MVvIZz
    [1] => qURRjy
    [2] => U7rIzu
    [3] => JNNJbi
)</code>
Copy after login
Copy after login

php7输出结果为:

<code>Array
(
    [0] => aaaaaa
    [1] => aaaaaa
    [2] => aaaaaa
    [3] => aaaaaa
)</code>
Copy after login
Copy after login

这是哪的问题?

回复内容:

<code>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/') );</code>
Copy after login
Copy after login

php5.6输出结果为:

<code>Array
(
    [0] => MVvIZz
    [1] => qURRjy
    [2] => U7rIzu
    [3] => JNNJbi
)</code>
Copy after login
Copy after login

php7输出结果为:

<code>Array
(
    [0] => aaaaaa
    [1] => aaaaaa
    [2] => aaaaaa
    [3] => aaaaaa
)</code>
Copy after login
Copy after login

这是哪的问题?

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template