PHP 转换特殊字符,例如 ş到 s,ţ到 t, ă到一个
我不知道如何命名我需要的内容,我想在 PHP 中将字符转换为更“正常”的字符集,例如:
ş to become s
ţ to become t,
ă to become a
我有罗马尼亚城镇名称,我想在 URL 中使用更多“正常”字符。 我想我想将罗马尼亚字符转换为美国字符(或者任何正确的名称)。
I am not sure how to name what I need, I want in PHP to convert characters into a more "normal" character set, like for example:
ş to become s
ţ to become t,
ă to become a
I am having Romanian town names and I want to use more "normal" characters in the URL.
I guess I want to convert Romanian characters to US (or whatever is the right name to call this).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会继续使用 PHP 提供的更复杂的音译系统。是关于 iconv 函数。
一个例子如下:
I would go on a more complex transliteration system which PHP gives us. Is about iconv function.
An example would be like the following:
查看 strtr ,它用特定的替换来替换特定的字符。
Have a look into strtr which replaces specific characters with specific replacements.
你想做的事情叫做音译。
intl 扩展中有一个 Transliterator(仅限 PHP 5.4): http://www.php.net/ Manual/en/transliterator.transliterate.php
示例:
What you want to do is called transliteration.
There is a Transliterator in the intl extension (PHP 5.4 only): http://www.php.net/manual/en/transliterator.transliterate.php
Example:
这个怎么样:
How about this:
您可以使用
str_replace
。You could use
str_replace
.正如这里已经告诉过的,你想要做的就是所谓的音译,但这在你的情况下可能并不总是能正确工作 - 你真正想做的是生成一个“slug”(不要问我为什么这么称呼.. .) 从人类提供的输入中使用 ie 在 url 中。
看一下这段代码: http://trac.symfony-project.org/browser/plugins/sfPropelActAsSluggableBehaviorPlugin/lib/sfPropelActAsSluggableBehaviorUtils.class.php 了解如何制作可靠的网址安全且人类可读的标识符。
As was already told here what you want to do is called transliteration, but this may not always work correctly in your case - what you really want to do is generate a 'slug' (don't ask me why it's called like that...) from human provided input to use i.e. in urls.
Take a look at this code : http://trac.symfony-project.org/browser/plugins/sfPropelActAsSluggableBehaviorPlugin/lib/sfPropelActAsSluggableBehaviorUtils.class.php to see how to make reliable url-safe and human-readable identifiers.