PHP implementation character Fu Jing URL implementation code

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.