Home  >  Article  >  Backend Development  >  Function to convert php characters to URL address_PHP tutorial

Function to convert php characters to URL address_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:55895browse

   英文:

 代码如下  

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

 代码如下  

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);
}

 代码如下  

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()函数了,利用它就可以直接转换了这个就不介绍了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/737699.htmlTechArticle英文: 代码如下 function tourl($str) { $str = preg_replace('/[^a-zA-Z0-9]+/', '-', $str); $str = trim($str, '-'); return strtolower($str); } 包含特殊字符: 代码如下...
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