获取 iconv 来转换我的字符串
我有以下字符串:
ᴰᴶ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最初认为这是您这边的编码问题,但是如果我从 soundcloud 源页面本地复制+粘贴这些字符:
并尝试 iconv 它们,我会得到与您相同的结果。这意味着数据是 UTF-8,但 iconv 无法将
ᴰ</code> 识别为
D
的“子级”。无法转换字符,它抱怨(在我看来有点误导)非法字符。
编辑:这似乎确实是真的。上标 D 不在 Unicode 上标和下标范围内,但它是一个 注音字符。这可能就是为什么它们无法映射回其“父”字母的原因。 此处是有关
ᴰ</code>的更多信息
据我所知,您唯一的选择是手动替换字符。
最原始的替换示例是
(请注意,您的源文件需要存储为 UTF-8 才能工作)
对于一个优雅的解决方案,您可以根据源字符和替换字符构建一个数组,并将其传递给
str_replace
调用。或者打电话给 DJ Bagi,告诉他把这些该死的字母搞清楚。您会注意到 Soundcloud 的 URL 构建器遇到了完全相同的问题。
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:
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" ofD
. 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
(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.