douchunjing6587 2011-11-11 10:41
浏览 13
已采纳

使我的脚本UTF-8兼容?

I have quite a long script which involves chopping lots of large text files into individual words and processing them.

I lowercase everything then remove all characters except for letters and spaces with:

$content=preg_replace('/[^a-z\s]/', '', $content); // Remove non-letters

This is then exploded and each word goes into an associated array as the key with the number of occurances as the value:

$words=array_count_values($content);

I want to convert the script to be able to work with languages other than English. Is PHP going to be OK with this? Can I use UTF-8 characters as array keys? And how would I preg_replace to remove everything except letters from any language? (All numbers, punctuation and random characters still need to be removed.)

  • 写回答

3条回答 默认 最新

  • dongyuchen9276 2011-11-11 10:51
    关注

    Yes you can use UTF-8 characters as keys (is there anything that can't be a key in a PHP array? :)). Your regexp might look something like:

    /\pL+/u
    

    EDIT: Sorry, should be:

    /[^\pL\p{Zs}]/u
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!