PHP to convert the URL into a short URL algorithm instance sharing

Source: Internet
Author: User
The short URL, as the name implies, is in the form of a relatively short URL. In Web 2.0 today, it has to be said that this is a trend. There are many similar services, with a short URL you can use a short URL instead of the original lengthy URL, so that users can easily share links, below to see how to implement this function in PHP, the need for friends can refer to.

Objective

Short URL service, may be a lot of friends are no longer unfamiliar, now most of the microblogging, mobile email reminders and other places already have a lot of application patterns, and occupy a certain market. It is estimated that many friends are using it now.

Benefits of Short Links:

1, the content needs;
2, user-friendly;
3, easy to manage.

Here is the algorithm for short URL conversion using PHP, the code is as follows:

Php

<?php//Short URL Generation algorithm class Shorturl {//Character descriptor public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop   QRSTUVWXYZ ";    public static function encode ($url) {$key = ' abc ';//Add salt $urlhash = MD5 ($key. $url);     $len = strlen ($urlhash);  The encrypted string is divided into 4 segments, 4 bytes per segment, each segment is calculated, and a total of four groups of short connections for ($i = 0; $i < 4; $i + +) {$urlhash _piece = substr ($urlhash, $i * $len            /4, $len/4); The bits of the segment and the 0x3fffffff do bit with, 0X3FFFFFFF represents the binary number of 30 1, that is, 30-bit after the encryption string is zero///here need to use the Hexdec () to convert the 16 binary string into 10 numeric type, otherwise the operation will not be normal $hex = Hexd       EC ($urlhash _piece) & 0x3fffffff;            The domain name fills in according to the demand $short _url = "http://t.cn/";  Generate a 6-bit short URL for ($j = 0; $j < 6; $j + +) {//will get the value with 0x0000003d,3d of 61, that is, the coordinate maximum value of charset $short _url. =                Self:: $charset [$hex & 0x0000003d];      After the loop, move hex to the right 5-bit $hex = $hex >> 5;    } $short _url_list[] = $short _url;  } return $short _url_list; }} $url = "http://www.sunbloger.com/"; $short = Shorturl::encode ($url);p Rint_r ($short);? >

Usually we use the first group in the four-group URLs.

It is important to note that this algorithm is irreversible, therefore, the usual practice is to save the short URL and the corresponding original URL into the database, when accessed, from the database to remove the matching original URL, through 301 or header to jump.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.