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 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧