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 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏