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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化