douduan2272 2015-01-15 11:10
浏览 108
已采纳

如何使用PHP格式的Mongo正则表达式转义特殊字符

I have tried below lines of code to get result of matching hotel name using Mongo regex in PHP format. But am not getting how to escape special characters:

$filter_array = array_merge($filter_array,
array('name' => array('$regex' => new MongoRegex("/".$hotel_name."/i"))));

Through this I am getting output for matching hotels but if suppose hotel name is like "ABC ( 3 Stars)" then am unable to get result for this kind of hotels as this hotel name is having special character "(" and ")".

How to escape such special character in hotel name?

  • 写回答

1条回答 默认 最新

  • du59131 2015-01-15 11:21
    关注

    It seems it uses PCRE style expressions, in that case preg_quote is used:

    new MongoRegex("/".preg_quote($hotel_name, '/')."/i")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试