获取 iconv 来转换我的字符串

发布于 2024-11-01 15:16:30 字数 431 浏览 4 评论 0原文

我有以下字符串:

ᴰᴶ Bagi

是否可以让 iconv 将其变成 DJ Bagi

首先我尝试使用:

$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

这导致了以下通知:

Notice: iconv() [function.iconv]: Detected an illegal character in input string 

在 PHP 网站上,我看到有人使用:

//IGNORE//TRANSLIT

虽然这阻止了我只收到的通知:

Bagi

I have the following string:

ᴰᴶ Bagi

Is it possible to let iconv make it into DJ Bagi?

First I tried with:

$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

Which resulted in the following notice:

Notice: iconv() [function.iconv]: Detected an illegal character in input string 

On the PHP site I saw someone using:

//IGNORE//TRANSLIT

While this prevents the notice I only get:

Bagi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一笑百媚生 2024-11-08 15:16:30

我最初认为这是您这边的编码问题,但是如果我从 soundcloud 源页面本地复制+粘贴这些字符:

ᴰᴶ Bagi

并尝试 iconv 它们,我会得到与您相同的结果。这意味着数据是 UTF-8,但 iconv 无法将 ᴰ<​​/code> 识别为 D的“子级”。无法转换字符,它抱怨(在我看来有点误导)非法字符。

编辑:这似乎确实是真的。上标 D 不在 Unicode 上标和下标范围内,但它是一个 注音字符。这可能就是为什么它们无法映射回其“父”字母的原因。 此处是有关ᴰ<​​/code>的更多信息

据我所知,您唯一的选择是手动替换字符。

最原始的替换示例是

str_replace("ᴰ", "D", $string);

(请注意,您的源文件需要存储为 UTF-8 才能工作)

对于一个优雅的解决方案,您可以根据源字符和替换字符构建一个数组,并将其传递给str_replace 调用。

或者打电话给 DJ Bagi,告诉他把这些该死的字母搞清楚。您会注意到 Soundcloud 的 URL 构建器遇到了完全相同的问题。

soundcloud.com/bagi 

I initially thought that this is an encoding problem on your end, but if I copy + paste those characters locally from the soundcloud source page:

ᴰᴶ Bagi

and try to iconv them, I get the same result as you do. That means that the data is UTF-8, but iconv does not recognize as a "child" of D. Unable to convert the character, it complains (a bit misleadingly IMO) about an illegal character.

Edit: This seems indeed true. Superscript D is not in the Unicode Superscripts and Subscripts range, but it's a phonetic character. That's probably why they can't be mapped back to their "parent" letter. Here is more info on

As far as I can see, your only choice is to replace the characters manually.

The most primitive example of a replace is

str_replace("ᴰ", "D", $string);

(note that your source file needs to be stored as UTF-8 for this to work)

For an elegant solution, you could build an array out of the source and replacement characters, and pass that to the str_replace call.

Or call DJ Bagi and tell him to get the damn letters straight. You will notice that Soundcloud's URL builder encountered exactly the same problem.

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