PHP 转换特殊字符,例如 ş到 s,ţ到 t, ă到一个

发布于 2025-01-05 16:27:28 字数 191 浏览 0 评论 0原文

我不知道如何命名我需要的内容,我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

始于初秋 2025-01-12 16:27:29

我会继续使用 PHP 提供的更复杂的音译系统。是关于 iconv 函数。

一个例子如下:

$city = iconv('UTF-8', 'ASCII//TRANSLIT', $city);
var_dump($city);

I would go on a more complex transliteration system which PHP gives us. Is about iconv function.

An example would be like the following:

$city = iconv('UTF-8', 'ASCII//TRANSLIT', $city);
var_dump($city);
彩扇题诗 2025-01-12 16:27:29

查看 strtr ,它用特定的替换来替换特定的字符。

echo strtr($string, "äåö", "aao");

Have a look into strtr which replaces specific characters with specific replacements.

echo strtr($string, "äåö", "aao");
戒ㄋ 2025-01-12 16:27:28

你想做的事情叫做音译。
intl 扩展中有一个 Transliterator(仅限 PHP 5.4): http://www.php.net/ Manual/en/transliterator.transliterate.php

示例:

$str = 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ';
$rule = 'NFD; [:Nonspacing Mark:] Remove; NFC';

$myTrans = Transliterator::create($rule); 
echo $myTrans->transliterate($str);

//aaaaaceeeeiiiinooooouuuuyy
//AAAAACEEEEIIIINOOOOOUUUUY

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:

$str = 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ';
$rule = 'NFD; [:Nonspacing Mark:] Remove; NFC';

$myTrans = Transliterator::create($rule); 
echo $myTrans->transliterate($str);

//aaaaaceeeeiiiinooooouuuuyy
//AAAAACEEEEIIIINOOOOOUUUUY
深空失忆 2025-01-12 16:27:28

这个怎么样:

$text = str_replace(array("ş","ţ","ă"),array("s","t","a"),$text);

How about this:

$text = str_replace(array("ş","ţ","ă"),array("s","t","a"),$text);
霓裳挽歌倾城醉 2025-01-12 16:27:28

您可以使用str_replace

$text = str_replace( array( 'ş', 'ţ', 'ă' ), array( 's', 't,', 'a' ), $text );

You could use str_replace.

$text = str_replace( array( 'ş', 'ţ', 'ă' ), array( 's', 't,', 'a' ), $text );
溺深海 2025-01-12 16:27:28

正如这里已经告诉过的,你想要做的就是所谓的音译,但这在你的情况下可能并不总是能正确工作 - 你真正想做的是生成一个“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文