下面是PHP的一個解決方案:
function make_uri($input, $max_length) {
if (function_exists('iconv')) {
$input = @iconv('UTF-8', 'ASCII//TRANSLIT', $input);
}
$lower = strtolower($input);
$without_special = preg_replace_all('/[^a-z0-9 ]/', '', $input);
$tokens = preg_split('/ +/', $without_special);
$result = '';
for ($tokens as $token) {
if (strlen($result.'-'.$token) > $max_length+1) {
break;
}
$result .= '-'.$token;
}
return substr($result, 1);
}
用法:
echo make_uri('In C#: How do I add "Quotes" around string in a ...', 500);
除非你需要的URI是鍵式,他們並不需要小。但是你應該指定一個最大值,以便這些網址可以很好地與代理服務器一起工作
這裏的一對URL一些信息長度:http://www.boutell。com/newfaq/misc/urllength.html – bchhun 2009-01-21 16:38:51
也許用他們的「英語發音」替換一些特殊字符,例如「#」=>「尖銳」,將允許製作更好的網址,並將C與C#區分開來(這很好,對吧?)?)? – Wookai 2009-01-21 17:17:05
是肯定的,除非#是不尖銳,這是一個不同的符號; p – Shawn 2009-01-22 03:53:09