php 将字符串转换为 Web 安全 URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4783802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting string into web safe URI
提问by Cudos
I have strings with special characters I want to convert. Usually I find all the special characters and their "web safe" counter parts manually and put them in arrays. Then I use preg_replace to replace each of the characters.
我有我想转换的特殊字符的字符串。通常我会手动找到所有特殊字符及其“网络安全”计数器部分并将它们放入数组中。然后我使用 preg_replace 来替换每个字符。
But I cannot help but thinking that there is an easier solution since it is error prune approach.
但我不禁想到有一个更简单的解决方案,因为它是错误修剪方法。
Here is an example of what I want:
这是我想要的一个例子:
Hans Günther -> hans-gunther
J?ren h?st -> jaeren-hoest
René ?gesen -> rene-aagesen
回答by o1iver
string urlencode ( string $str )
字符串 urlencode ( 字符串 $str )
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urlencode.php
Actually here is a great post about converting text like your example above to nice url-safe strings (probably better for you than the above function):
实际上,这是一篇关于将上面示例中的文本转换为漂亮的 url-safe 字符串的好文章(可能比上述函数更适合您):
http://cubiq.org/the-perfect-php-clean-url-generator
http://cubiq.org/the-perfect-php-clean-url-generator
setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
Here are examples of what it does:
以下是其作用的示例:
echo toAscii("Mess'd up --text-- just (to) stress /test/ ?our! `little` \clean\ url fun.ction!?-->");
returns: messd-up-text-just-to-stress-test-our-little-clean-url-function
echo toAscii("Perché l'erba è verde?", "'"); // Italian
returns: perche-l-erba-e-verde
echo toAscii("Peux-tu m'aider s'il te pla?t?", "'"); // French
returns: peux-tu-m-aider-s-il-te-plait
echo toAscii("T?nk efter nu – f?rr'n vi f?ser dig bort"); // Swedish
returns: tank-efter-nu-forrn-vi-foser-dig-bort
echo toAscii("àá??????èéê?ìí??D?òó???ùú?üY?àáa?????èéê?ìí??e?òó???ùú?üy?");
returns: aaaaaaaeceeeeiiiidnooooouuuuyssaaaaaaaeceeeeiiiidnooooouuuuyy
echo toAscii("Custom`delimiter*example", array('*', '`'));
returns: custom-delimiter-example
echo toAscii("My+Last_Crazy|delimiter/example", '', ' ');
returns: my last crazy delimiter example
If you want to convert something like ? to ae and etc you can use a script like this (sorry, don't know about a better way of doing this):
如果你想转换类似的东西?对于ae等,您可以使用这样的脚本(对不起,不知道这样做的更好方法):
setlocale(LC_ALL, 'de_DE');
$replace = array(
'illegal' => array('/?/', '/?/', '/ü/', '/?/', '/?/', '/ü/', '/?/', '/é/'),
'legal' => array('Ae', 'Oe', 'Ue', 'ae', 'oe', 'ue', 'Aa', 'e')
);
$string = 'ich h?tte gerne br?techen Mein Name ist ?les ?lex';
echo preg_replace($replace['illegal'], $replace['legal'], $string);
//Output: "ich haette gerne broetechen Mein Name ist Oeles Aalex"
You can obviously put it all togetherlike this (only converting ü->ue etc, just add more to the first preg_replace):
您显然可以像这样将它们放在一起(仅转换 ü->ue 等,只需在第一个 preg_replace 中添加更多):
setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = preg_replace(array('/?/', '/?/', '/ü/', '/?/', '/?/', '/ü/'), array('Ae', 'Oe', 'Ue', 'ae', 'oe', 'ue'), $str);
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
$text = "H?tten Sie gerne viele Br?tchen? Wenn ja dann einfach *!@#$%^&*()eingeben...";
echo toAscii($text);
//OUTPUT: haetten-sie-gerne-viele-broetchen-wenn-ja-dann-einfach-eingeben
回答by Vindic
The code above didn't work for the characters accented.
上面的代码不适用于重音字符。
For this purpose i had to replace myselt this caracters by the equivalent without accent:
为此,我不得不用不带口音的等价物替换 myselt 这个字符:
function replace_accent($str)
{
$a = array('à', 'á', '?', '?', '?', '?', '?', '?', 'è', 'é', 'ê', '?', 'ì', 'í', '?', '?', 'D', '?', 'ò', 'ó', '?', '?', '?', '?', 'ù', 'ú', '?', 'ü', 'Y', '?', 'à', 'á', 'a', '?', '?', '?', '?', '?', 'è', 'é', 'ê', '?', 'ì', 'í', '?', '?', '?', 'ò', 'ó', '?', '?', '?', '?', 'ù', 'ú', '?', 'ü', 'y', '?', 'ā', 'ā', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'ē', 'ē', '?', '?', '?', '?', '?', '?', 'ě', 'ě', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'ī', 'ī', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'ń', '?', '?', '?', 'ň', '?', 'ō', 'ō', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'ū', 'ū', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'ǎ', 'ǎ', 'ǐ', 'ǐ', 'ǒ', 'ǒ', 'ǔ', 'ǔ', 'ǖ', 'ǖ', 'ǘ', 'ǘ', 'ǚ', 'ǚ', 'ǜ', 'ǜ', '?', '?', '?', '?', '?', '?');
$b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
return str_replace($a, $b, $str);
}
function toURI($str, $replace = array(), $delimiter = '-')
{
if(!empty($replace))
{
$str = str_replace((array) $replace, ' ', $str);
}
$clean=replace_accent($str);
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
This one make the job done.
这使工作完成。