Home > Article > Backend Development > PHP uses strings to convert between short URLs and numbers
This article mainly introduces the method of converting between PHP short URLs and numbers, involving the skills of PHP operating strings, and has certain reference value. Friends in need can refer to the following
Examples of this article The method of converting between PHP short URL and numbers.
The specific implementation method is as follows:
<?php /** * 将数字转为短网址代码 * * @param int $number 数字 * @return string 短网址代码 */ function generate_code($number) { $out = ""; $codes = "abcdefghjkmnpqrstuvwxyz23456789ABCDEFGHJKMNPQRSTUVWXYZ"; while ($number > 53) { $key = $number % 54; $number = floor($number / 54) - 1; $out = $codes{$key}.$out; } return $codes{$number}.$out; } /** * 将短网址代码转为数字 * * @param string $code 短网址代码 * @return int 数字 */ function get_num($code){ $codes = "abcdefghjkmnpqrstuvwxyz23456789ABCDEFGHJKMNPQRSTUVWXYZ"; $num = 0; $i = strlen($code); for($j=0;$j<strlen($code);$j++){ $i--; $char = $code{$j}; $pos = strpos($codes,$char); $num += (pow(54, $i) * ($pos + 1)); } $num--; return $num; }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.
Related recommendations:
PHP generates excel files to the specified directory
Inheritance and extension operation skills of PHP classes
PHP method to obtain audio file information
The above is the detailed content of PHP uses strings to convert between short URLs and numbers. For more information, please follow other related articles on the PHP Chinese website!