简体   繁体   English

更改 iconv() 替换字符

[英]Change iconv() replacement character

I am using iconv() to replace characters for an api request like this:我正在使用iconv()来替换 api 请求的字符,如下所示:

$text = iconv("UTF-8", "ASCII//TRANSLIT", $text);

This works great, however when an unknown character is encountered it gets replaced by a ?这很好用,但是当遇到未知字符时,它会被替换为? . . Is there any straightforward way to change the substitution character to something else?有什么直接的方法可以将替换字符更改为其他字符? Say a space?说空格? I know about the setting for mb functions mb_substitute_character() - but this doesn't apply to iconv() .我知道 mb 函数mb_substitute_character()的设置 - 但这不适用于iconv()

Example:例子:

$text = '? € é î ü π ∑ ñ';
echo iconv("UTF-8", "ASCII//TRANSLIT", $text), PHP_EOL;

Output: Output:

? EUR e i u ? ? n

Desired Output:所需 Output:

? EUR e i u     n

AFAIK there's no translit function that lets you specify your own replacement character, but you can work around it by implementing your own simple escaping.据我所知,没有可让您指定自己的替换字符的转换 function,但您可以通过实现自己的简单 escaping 来解决它。

function my_translit($text, $replace='!', $in_charset='UTF-8', $out_charset='ASCII//TRANSLIT') {
    // escape existing ?
    $res = str_replace(['\\', '?'], ['\\\\', '\\?'], $text);
    // translit
    $res = iconv($in_charset, $out_charset, $res);
    // replace unescaped ?
    $res = preg_replace('/(?<!\\\\)\\?/', $replace, $res);
    // unescape
    return str_replace(['\\\\', '\\?'], ['\\', '?'], $res);
}

$text = '? € é î ü π ∑ ñ \\? \\\\? \\\\\\?';
var_dump(
    $text,
    my_translit($text)
);

Result:结果:

string(36) "? € é î ü π ∑ ñ \? \\? \\\?"
string(29) "? EUR ! ! ! p ! ! \? \\? \\\?"

I'm not certain why the transliteration output is different on my system, but the character replacement works.我不确定为什么音译 output 在我的系统上不同,但字符替换有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM