dongsaohu6429 2014-02-14 01:33
浏览 751
已采纳

从字符串中删除大括号

I've found many examples on the internet, but still can't find a solution for this. I got a PHP page that needs to parse a HTML+JS page and needs to remove everything included between { } and also the brackets itself. The point is that the content of the brackets must be [A-Za-z0-9_] and not anything else (like i did so far).

This is my current replace:

$this->content = preg_replace("/\{((?:[^{}]++|\{(?1)\})++)\}/", "", $this->content);

So far now this works pretty good, the point is that doesn't search for right contents between brackets and removes them no matter what's inside (atm it's removing JS functions from the content of the page that is parsing).

What do I've to modify to add that kind of character restriction to the content of the reg-ex?

Thanks

  • 写回答

3条回答 默认 最新

  • dougong7850 2014-02-14 01:37
    关注

    Try to use this pattern:

    \{[a-zA-Z0-9_]+?\}
    

    Better to use lazy quantification with ? sign at the end of repetitions number.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部