English:
| The code is as follows |
Copy Code |
|
function Tourl ($STR) {
$str = preg_replace ('/[^a-za-z0-9]+/', '-', $str);
$str = Trim ($str, '-');
Return Strtolower ($STR);
} |
Contains special characters:
| The code is as follows |
Copy Code |
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);
} |
UrlEncode function This function can solve the problem of Baidu get and the Chinese garbled
| The code is as follows |
Copy Code |
|
Header (' content-type:text/html; Charset=utf-8 ');
Echo ' <br/> Baidu keyword URL code conversion example: <br/> ';
$urlBaidu = ' http://www.baidu.com/s?wd=%D6%D0%CE%C4 ';
$arrParse = Parse_url ($urlBaidu);
Parse_str ($arrParse [' query '], $output);
Echo ' garbled: '. $output [' WD ']. ' <br/> ';
Convert gb2312 encoding to UTF-8 encoding
$strUTF 8 = mb_convert_encoding ($output [' WD '], ' utf-8 ', ' gb2312 ');
echo ' Readable code: '. $strUTF 8. ' <br/> ';
Echo ' <br/> Google keyword URL encoding conversion example: <br/> ';
$urlGoogle = ' http://www.google.cn/search?hl=zh-CN&source=hp&q=%E4%B8%AD%E6%96%87&aq=f&oq= ';
$arrParse = Parse_url ($urlGoogle);
Parse_str ($arrParse [' query '], $output);
Google keyword URL code is utf-8, no more conversion
echo ' Readable code: '. $output [' Q ']. ' <br/> ';
|
Summary, the individual prefers to use PHP with UrlEncode () function, above this is also a solution to our URL in Chinese when Baidu and GG sometimes garbled problem solution.