drt5813 2015-05-30 19:04
浏览 223
已采纳

允许空格,unicode字母,数字,下划线,短划线和逗号?

I'm pretty new at the subject preg and I'm using this preg_match condition to check if the user has entered whitespace, unicode letters, digits, underscore or dash:

if(preg_match("/[^\040\pL\pN_-]/u", $term)) {

But now I wanted to allow a comma. So I tried this:

if(preg_match("/[^\040\pL\pN,_-]/u", $term)) {

And it actually works and I just wanted to know why. I just want to understand it better. Why does it have to be ,_- and not -_, for example to allow the comma?

I would really appreciate if someone could explain this to me step by step.

  • 写回答

2条回答 默认 最新

  • dsv38843 2015-05-30 19:08
    关注

    This is because - is used for ranges in square brackets([] -> character classes). And as from the manual: indicates character range, example: 0-9 or a-z.

    So as long as you put it at the end you're fine and don't have to escape it. In all other cases you have to escape it with a backslash e.g. \-.

    Means:

    ,_-  //At the end
    _,-  //At the end
    \-,_ //Escape it
    \-_, //Escape it
    ,\-_ //Escape it
    _\-- //Escape it
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用