dotdx80642 2013-01-13 18:18
浏览 190
已采纳

匹配字符串的正则表达式包含文本双引号和方括号

I am trying to extract the text between two strings with double quotes and square brackets. [gallery ids=" and "]

The format I have is:

[gallery ids="55,57,56,58,59"]

Which I would like to turn in to

55,57,56,58,59

I have tried every variety of pattern I've come across but have had no luck. Can anyone tell me what pattern would achieve this using PHP's regex functions?

  • 写回答

1条回答 默认 最新

  • doutangtan6386 2013-01-13 18:21
    关注

    That should work for you:

    $string = '[gallery ids="55,57,56,58,59"]';
    if (preg_match('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
        echo $m[1];
    }
    

    or if you want to match more than one string like that in the text, then just use preg_match_all:

    if (preg_match_all('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
        print_r($m[1]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流