doucaishi0077 2014-09-18 09:10
浏览 36
已采纳

ICU:Transliterate然后删除所有非字母数字字符

Can it be done with ICU without falling back to regex?

Currently I normalize filenames like this:

protected function normalizeFilename($filename)
{
    $transliterator = Transliterator::createFromRules(
        'Any-Latin; Latin-ASCII; [:Punctuation:] Remove;'
    );
    $filename = $transliterator->transliterate($filename);
    $filename = preg_replace('/[^A-Za-z0-9_]/', '', $filename);
    return $filename;

}

Can I get rid of regular expression here and do everything with ICU calls?

  • 写回答

1条回答 默认 最新

  • dongxu1668 2014-09-25 21:46
    关注

    Use the correct tool for the job

    I don't see anything wrong with what you're doing now.

    ICU transliteration is first and foremost language oriented. It tries to preserve meaning.

    Regular expressions, on the other hand, can manipulate characters in detail, giving you the assurance that the file name is restricted to the selected characters.

    The combination is perfect, in this case.

    I have, of course, looked for a solution to your question. But to be honest, I couldn't find something that would work on all possible inputs.

    For instance, not all characters, we would consider punctuation marks, are removed by [:Punctuation:] Remove;. Try the Russian name: Корнильев, Кирилл. After applying your id it becomes: Kornilʹev Kirill. Clearly that's not a punctuation mark, but you don't want it in your file name.

    So I would advice to use the correct tool for the job:

    1. Use ICU to get the best ASCII enquivalent. Only using Latin-ASCII; as the id will do. Nice and simple.
    2. Then use a regular expression, just like you did, to make sure you're left with only the characters you need.

    There is really nothing wrong with this.

    PS: Personally I think the person, or persons, who wrote the ICU user guide should not be complimented on a job well done. What a mess.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用