dongmeiran609914 2014-12-31 14:03
浏览 18
已采纳

PHP爆炸所有方括号

how do i explode the square brackets and then replace them with some text?

i tried the following code, but it seems only recognize the first two:

Regex code:

/(\[[\w\s]+\])/g

anyone can suggest what needs to be changed in the regex code?

$data = "[Foo Bar],[Suganthan],['Test1',1,5.09,12.50, 7.41]";

but it only finds: [Foo Bar],[Suganthan]

preg_replace('/(\[[\w\s]+\])/', 'replaced', $data);
  • 写回答

3条回答 默认 最新

  • douduiwei2831 2014-12-31 14:06
    关注

    You can use this regex to capture all the square bracket contents:

    (\[[^]]+\])
    

    RegEx Demo

    Code:

    $data = preg_replace('/\[[^]]+\]/', 'replaced', $data);
    

    If you only want to find content from inside the [ and ] then use lookarounds as in this regex:

    (?<=\[)[^]]+(?=\])
    

    And use code as:

    $data = preg_replace('/(?<=\[)[^]]+(?=\])/', 'replaced', $data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表
  • ¥15 DbVisualizer Pro 12.0.7 sql commander光标错位 显示位置与实际不符