A PHP function to generate short URLs

巴扎黑
Release: 2016-11-11 10:19:03
Original
1070 people have browsed it

Everyone is familiar with short URLs, especially applications on Weibo that have made it popular. In fact, it is very simple to implement this function.

Java code

0){  
        $s= $result%62;  
        if($s>35){  
            $s= chr($s+61);  
        } elseif($s>9 && $s<=35){  
            $s= chr($s+ 55);  
        }  
        $sUrl.= $s;  
        $result= floor($result/62);  
    }  
    return $sUrl;  
}  
   
$url = 'www.qttc.net';  
$sUrl = urlShort($url);  
   
echo '';  
echo '网址:'.$url.'
'; echo '短网址:'.$sUrl; ?>
Copy after login


The above result output:
Website: www.qttc.net
Short URL: SwOOy3
The short URL can be stored in the database and make a mapping relationship. Together with nginx rewriting rules, short URL generation, restoration, and jump functions can be realized.


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!