Home >Backend Development >PHP Tutorial >A PHP function to generate short URLs
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.
<?php /** * 短网址 */ function urlShort($url){ $url= crc32($url); $result= sprintf("%u", $url); $sUrl= ''; while($result>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 '<meta charset="utf-8" />'; echo '网址:'.$url.'<br />'; echo '短网址:'.$sUrl; ?>
The above introduces a PHP function to generate short URLs, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.