用于删除重音符号和其他自定义替换的音译器

问题描述 投票:0回答:1

我正在尝试编写一个音译器,删除字母中的重音符号,并将逗号、空格和冒号更改为

-
,将句点更改为
_

这是我一直在尝试的,但是不行,对象是

NULL

$transL =Transliterator::createFromRules('
    :: Any-Latin;
    :: Latin-ASCII;
    :: NFD;
    :: [:Nonspacing Mark:] Remove;
    :: NFC;
    :: [, ] > "-";
    :: [\u0020] > "-";
    :: [:Colon:] > "-";
    :: [. ] > "_";
',Transliterator::FORWARD);

var_dump($transL); # NULL
php transliteration
1个回答
0
投票

您可以使用

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.