dongyan6503 2014-11-27 07:47
浏览 321
已采纳

正则表达式替换标点符号

I've been trying for a few hours to get this to work to the effect I need but nothing works quite like it should. I'm building a discussion board type thing and have made a way to tag other users by putting @username in the post text.

Currently I have this code to strip anything that wouldn't be part of the username once the tags have already been pulled out of the entire text:

$name= preg_replace("/[^A-Za-z0-9_]/",'',$name);

This works well because it correct captures names that are for example (@username), @username:, @username, some text etc. (so to remove the ,, :, and )).

HOWEVER, this does not work when the user has non-ascii characters in their username. For example if it's @üsername, the result of that line above gives sername which is not useful.

IS there a way using preg_replace to still strip these additional punctuation, but retain any non-ascii letters?

Any help is much appreciated :)

  • 写回答

2条回答 默认 最新

  • dsriya5471 2014-11-27 07:49
    关注

    To detect punctuation characters, you can use unicode property \p{P} instead:

    $name = preg_replace('/[\p{P} ]+/', '', $name);
    

    RegEx Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?