在PHP中将2种外观相似的德语字符转换为相同的ASCII字符串 [英] Convert 2 similarly-looking German characters of different kinds to same ASCII string in PHP

查看:95
本文介绍了在PHP中将2种外观相似的德语字符转换为相同的ASCII字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个字符串:

$str1 = 'Ö';
$str2 = 'Ö';
$e1 = mb_detect_encoding($str1);
$e2 = mb_detect_encoding($str2);
var_dump($str1);
var_dump($str2);
echo 'e1: '.$e1.', e2: '.$e2;

结果是:

string(3) "Ö"
string(2) "Ö"
e1: UTF-8, e2: UTF-8

似乎它们不仅是德语字符,而且每个字符都不相同,因此以这种方式将它们转换为ASCII

It seems that they are not only German characters but also each of them is different so converting them to ASCII this way

PHP :用UTF-8字符串中最接近的7位ASCII等效字符替换变音符号

不会产生相等的结果.有没有办法将这两个字符串都转换为这些ASCII形式之一BNOEBNO?

doesn't produce equal results. Is there a way to convert both of these strings to one of these ASCII forms BNOE or BNO?

我知道也许我可以从这两者中复制Ö并包含在strtr搜索和替换数组中,但是我不知道如何再现所有与第一个Ö相同的编码字符.

I know that maybe I could copy Ö from both and include in strtr search and replace array but I don't know how to reproduce all the charactes encoded the same way the first Ös are.

推荐答案

您可以先使用iconv将输入转换为utf-8,然后再将转换应用于ASCII.要检测当前的编码,可以使用mb_detect_encoding.

You could first convert your input to utf-8 using iconv and then apply your conversion to ASCII. To detect the current encoding you can use mb_detect_encoding.

$aUTF8 = iconv(mb_detect_encoding($a, 'UTF-8, ISO-8859-1', true), 'UTF-8', $a);
$bUTF8 = iconv(mb_detect_encoding($b, 'UTF-8, ISO-8859-1', true), 'UTF-8', $b);

$aASCII = iconv("utf-8", "ascii//TRANSLIT", $aUTF8);
$bASCII = iconv("utf-8", "ascii//TRANSLIT", $bUTF8);

请注意,您可能必须将其他编码添加到mb_detect_encoding的编码列表中.

Please note that you might have to add additional encodings to the encoding list of mb_detect_encoding.

这篇关于在PHP中将2种外观相似的德语字符转换为相同的ASCII字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆