dpp80346 2015-06-05 19:22
浏览 54

Preg_Replace不能处理法语字符 - PHP

I've searched for a while so hopefully this is not a question that is asked many times already.

I'm trying to program on php a script that would remove stop words from a string, and then explode it in an array of words. The stop words could be in English or French.

Currently the following is not working for me as it doesn't remove French characters:

$needles=array(
'/\bil\b/i', 
'/\bla\b/i', 
'/\ble\b/i', 
'/\b'. htmlentities('à') .'\b/i'
);
print_r($needles);

$result=preg_replace($needles, "", htmlentities("il y à trois personne dans la salle à manger"));
print_r($result);

The output removes everything but not the french character: à

  • 写回答

1条回答 默认 最新

  • drvvvuyia15070493 2015-06-05 19:45
    关注

    As it has been noted in the comments, htmlentities('à') will give you [3] => /\bà\b/i. It won't match your letter.

    Instead, use the à with u flag to enable Unicode in the pattern:

    '/\bà\b/iu'
    

    See demo

    IDEONE demo:

    <?php
    $needles=array(
    '/\bil\b/i', 
    '/\bla\b/i', 
    '/\ble\b/i', 
    '/\bà\b/iu'
    );
    print_r($needles);
    
    $result=preg_replace($needles, "", "il y à trois personne dans la salle à manger");
    print_r($result);
    

    Output:

    y  trois personne dans  salle  manger
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?