PHP URL shortened code (generate short URLs) _php tutorial

Source: Internet
Author: User
Short URLs now used more than a lot of jump site will be generated, such as the original micro-blog has this function, I would like to recommend a PHP URL to shorten the code

Each URL is replaced by 6 characters, and (32^6) can have a maximum of 1,073,741,824 short URLs. Of course, you can also record more detailed information such as access history, creation time, etc. If it is not enough, it can also be deleted for a long time.

The code is as follows Copy Code

function Shorturl ($input) {

$base = Array (

' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ',

' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ',

' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' X ',

' Y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '

);


$hex = MD5 ($input);

$hexLen = strlen ($hex);

$subHexLen = $hexLen/8;

$output = Array ();


for ($i = 0; $i < $subHexLen; $i + +) {

$subHex = substr ($hex, $i * 8, 8);

$int = 0X3FFFFFFF & (1 * (' 0x '. $subHex));

$out = ";


for ($j = 0; $j < 6; $j + +) {

$val = 0x0000001F & $int;

$out. = $base 32[$val];

$int = $int >> 5;

}


$output [] = $out;

}


return $output;

}


Sample code to Test/use the above function:

$input = ' HTTP://WWW.BKJIA.C0M/1 ';

$output = Shorturl ($input);


echo "Input: $inputn";

echo "Output: {$output [0]}n];

echo "{$output [1]}n];

echo "{$output [2]}n];

echo "{$output [3]}n];

echo "n";


$input = ' HTTP://WWW.BKJIA.C0M/2 ';

$output = Shorturl ($input);


echo "Input: $inputn";

echo "Output: {$output [0]}n];

echo "{$output [1]}n];

echo "{$output [2]}n];

echo "{$output [3]}n];

echo "n";


Output:

Input:http://www.bkjia.c0m/1

output:h0xg4r

Bdr3tw

Osk2d3

4azfqa


Input:http://www.bkjia.c0m/2

Output:tm5kxb

Ceoj2s

Yw3dvl

Nrmrxl

http://www.bkjia.com/PHPjc/632729.html www.bkjia.com true http://www.bkjia.com/PHPjc/632729.html techarticle short URLs are now used more than a lot of jump site will be generated, like the original Micro Bo also has this function, below I will recommend a PHP URL shortened code each URL with 6 characters instead of ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.