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条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图