我正在尝试编写一个音译器,删除字母中的重音符号,并将逗号、空格和冒号更改为
-
,将句点更改为_
。
这是我一直在尝试的,但是不行,对象是
NULL
。
$transL =Transliterator::createFromRules('
:: Any-Latin;
:: Latin-ASCII;
:: NFD;
:: [:Nonspacing Mark:] Remove;
:: NFC;
:: [, ] > "-";
:: [\u0020] > "-";
:: [:Colon:] > "-";
:: [. ] > "_";
',Transliterator::FORWARD);
var_dump($transL); # NULL
intl_get_error_message()
函数来获取导致 NULL
的错误:
transliterator_create_from_rules: unable to create ICU transliterator from rules (parse error at offset 111, after "; :: [, ] >", before or at " "-"; :: [\"): U_UNQUOTED_SPECIAL
似乎是由
"-"
引起的,用转义的 -
替换修复了::: [, ] > \-;
。
<?php
$transL =Transliterator::createFromRules('
:: Any-Latin;
:: Latin-ASCII;
:: NFD;
:: [:Nonspacing Mark:] Remove;
:: NFC;
:: [, ] > \-;
:: [\u0020}] > \-;
:: [. ] > \_;
',Transliterator::FORWARD);
if (($error = intl_get_error_message()) === 'U_ZERO_ERROR') {
$test = 'Foo,Bar';
var_dump($transL->transliterate($test));
} else {
var_dump($error);
}