Home  >  Article  >  Backend Development  >  Use PHP to implement short URL function

Use PHP to implement short URL function

WBOY
WBOYOriginal
2016-07-25 08:42:511011browse
  1. function shorturl($url){
  2. $length = strlen($url);
  3. if($length > 45){
  4. $length = $length - 30;
  5. $first = substr($url, 0, -$length);
  6. $last = substr($url, -15);
  7. $new = $first."[ ... ]".$last;
  8. return $new;
  9. }else{
  10. return $url;
  11. }
  12. }
  13. ?>
  14. // USAGE
  15. $longurl= "http://www.google.com/search?q=symfony+project&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:tr:official&client=firefox-a";
  16. $shorturl = shorturl($longurl);
  17. echo "$shorturl";
  18. ?>
复制代码

PHP


Statement:
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