Home  >  Article  >  php教程  >  php字符转URL地址的函数

php字符转URL地址的函数

WBOY
WBOYOriginal
2016-06-13 09:41:421417browse

   英文:

 代码如下  

function tourl($str) {
  $str = preg_replace('/[^a-zA-Z0-9]+/', '-', $str);
  $str = trim($str, '-');
  return strtolower($str);
}

  包含特殊字符:

 代码如下  

function strtourl($str) {
  $str = htmlentities($str, ENT_QUOTES, "UTF-8");
  $str = preg_replace('/&([a-z]{1,2})(?:acute|lig|grave|ring|tilde|uml|cedil|caron);/i','1',$str);
  $str = html_entity_decode($str, ENT_QUOTES, "UTF-8");
  $str = preg_replace('/[^a-z0-9]+/i', '-', $str);
  return strtolower($str);
}

  除了上面方法php提供了urlencode()、urldecode()、rawurlencode()、rawurldecode()函数了,利用它就可以直接转换了这个就不介绍了。

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