drb88830 2011-06-04 23:20
浏览 95
已采纳

PHP preg_replace - String开始使用正斜杠

Trying to use preg_replace to find strings that begin and end with a forward-slash, for example "/Harry/". (has to be preg_replace)

I also need to ignore case and ensure it is an individual word. So far I have the following but I am having little luck getting it to work :(

$old_words[0] = '/\b\/Harry\/\b/i';

$new_words[0] = 'Wizard';

$chat = preg_replace($old_words, $new_words, $chat);
  • 写回答

2条回答 默认 最新

  • doumei4964 2011-06-05 00:52
    关注

    Should your problem really just be the word boundaries \b, then you could try some auxiliary assertions instead:

    $old_words[0] = '#(?<!\w)/Harry/(?!\w)#i';
    

    That basically ensures that no letters can occur before or after the slashes.

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

报告相同问题?

悬赏问题

  • ¥20 数学建模数学建模需要
  • ¥15 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决