使用正则表达式和php preg_match_all在括号之间获取字符串

使用正则表达式和php preg_match_all在括号之间获取字符串,php,regex,preg-match-all,Php,Regex,Preg Match All,我有几个文本与自定义宏标记在它。我想解析这些标记的内容,但我想以不同的方式对待其中包含参数的标记 我需要用这些括号内容构造有效的URL 示例: 这是我的文字: {gallery}events/2016-02-18-Sunny-Sport-Day,single=IMG_0336.jpg,salign=left{/gallery} 嘿!我们有一个艰难但有趣的比赛,度过了美好的一天。我们的 该队进行了一场精彩的比赛,最终获得了第二名 {gallery}events/2016-02-18-Sunny-S

我有几个文本与自定义宏标记在它。我想解析这些标记的内容,但我想以不同的方式对待其中包含参数的标记

我需要用这些括号内容构造有效的URL

示例:

这是我的文字:

{gallery}events/2016-02-18-Sunny-Sport-Day,single=IMG_0336.jpg,salign=left{/gallery}

嘿!我们有一个艰难但有趣的比赛,度过了美好的一天。我们的 该队进行了一场精彩的比赛,最终获得了第二名

{gallery}events/2016-02-18-Sunny-Sport-Day{/gallery}

{gallery}团队/成员{/gallery}

因此,我需要提取{gallery}标记之间字符串的路径部分,但我不想将这些部分与参数匹配,例如“single=IMG_0336.jpg,salign=left”,因为它们是分开处理的

我需要做以下事情:

/,single=(.*?),/
{gallery}events/2016-02-18-Sunny-Sport-Day,single=IMG_0336.jpg,salign=left{/gallery}

变成

第一个输出:events/2016-02-18-Sunny-Sport-Day

第二个输出:IMG_0336.jpg

{gallery}events/2016-02-18-Sunny-Sport-Day{/gallery}

变成

活动/2016-02-18-Sunny-Sport-Day

尝试了以下正则表达式:

/\{gallery\}(.*?)(?!single=)\{\/gallery\}/
但它总是匹配整个字符串,包括单个参数

为了获得单个参数的内容,我尝试了以下方法:

/,single=(.*?),/
这只适用于单个参数,但我不知道如何将所有内容组合在一起

结论:

在PHP环境中,我希望有两个数组作为输出。第一个仅由以下文件夹组成:

  • 活动/2016-02-18-Sunny-Sport-Day
  • 团队/成员
以及由单个文件路径组成的第二阵列:

  • events/2016-02-18-Sunny-Sport-Dayist/IMG_0336.jpg
    • 像这样的东西

      <?php
      $str=getstr();
      preg_match_all('/\{gallery\}(.*?)\{\/gallery\}/u',$str,$matches);
      $parsed=[];
      foreach($matches[1] as $match){
          $tmp=[];
          $match=explode(',',$match);
          foreach($match as $tmp2){
              $tmp2=explode("=",$tmp2);
              assert(count($tmp2)<=2);
              if(count($tmp2)>1){
                  $tmp[$tmp2[0]]=$tmp2[1];
              }else{
                  $tmp[]=$tmp2[0];
              }
          }
          $parsed[]=$tmp;
      }
      var_dump($parsed);
      
      
      function getstr():string{
          $str=<<<'STR'
      {gallery}events/2016-02-18-Sunny-Sport-Day,single=IMG_0336.jpg,salign=left{/gallery}
      
      Hey there! We had a great day with a tough but funny competition. Our team had a great race and was able to finish in second place.
      
      {gallery}events/2016-02-18-Sunny-Sport-Day{/gallery}
      
      {gallery}team/members{/gallery}
      STR;
      return $str;
      }
      
      像这样的

      <?php
      $str=getstr();
      preg_match_all('/\{gallery\}(.*?)\{\/gallery\}/u',$str,$matches);
      $parsed=[];
      foreach($matches[1] as $match){
          $tmp=[];
          $match=explode(',',$match);
          foreach($match as $tmp2){
              $tmp2=explode("=",$tmp2);
              assert(count($tmp2)<=2);
              if(count($tmp2)>1){
                  $tmp[$tmp2[0]]=$tmp2[1];
              }else{
                  $tmp[]=$tmp2[0];
              }
          }
          $parsed[]=$tmp;
      }
      var_dump($parsed);
      
      
      function getstr():string{
          $str=<<<'STR'
      {gallery}events/2016-02-18-Sunny-Sport-Day,single=IMG_0336.jpg,salign=left{/gallery}
      
      Hey there! We had a great day with a tough but funny competition. Our team had a great race and was able to finish in second place.
      
      {gallery}events/2016-02-18-Sunny-Sport-Day{/gallery}
      
      {gallery}team/members{/gallery}
      STR;
      return $str;
      }
      

      此方法将提取所需的子字符串,并按要求准备输出数据:

      PHP代码:()

      输出:

      array (
        0 => 
        array (
          0 => '{gallery}events/2016-02-18-Sunny-Sport-Day,single=IMG_0336.jpg,salign=left{/gallery}',
          1 => '{gallery}events/2016-02-18-Sunny-Sport-Day{/gallery}',
          2 => '{gallery}team/members{/gallery}',
        ),
        1 => 
        array (
          0 => 'events/2016-02-18-Sunny-Sport-Day',
          1 => 'events/2016-02-18-Sunny-Sport-Day',
          2 => 'team/members',
        ),
        2 => 
        array (
          0 => 'IMG_0336.jpg',
          1 => '',
          2 => '',
        ),
      )
      
      ---
      // Folders only array:
      array (
        0 => 'events/2016-02-18-Sunny-Sport-Day',
        1 => 'events/2016-02-18-Sunny-Sport-Day',
        2 => 'team/members',
      )
      
      ---
      // Path + Image files array:
      array (
        0 => 'events/2016-02-18-Sunny-Sport-Day/IMG_0336.jpg',
      )
      

      此方法将提取所需的子字符串,并按要求准备输出数据:

      PHP代码:()

      输出:

      array (
        0 => 
        array (
          0 => '{gallery}events/2016-02-18-Sunny-Sport-Day,single=IMG_0336.jpg,salign=left{/gallery}',
          1 => '{gallery}events/2016-02-18-Sunny-Sport-Day{/gallery}',
          2 => '{gallery}team/members{/gallery}',
        ),
        1 => 
        array (
          0 => 'events/2016-02-18-Sunny-Sport-Day',
          1 => 'events/2016-02-18-Sunny-Sport-Day',
          2 => 'team/members',
        ),
        2 => 
        array (
          0 => 'IMG_0336.jpg',
          1 => '',
          2 => '',
        ),
      )
      
      ---
      // Folders only array:
      array (
        0 => 'events/2016-02-18-Sunny-Sport-Day',
        1 => 'events/2016-02-18-Sunny-Sport-Day',
        2 => 'team/members',
      )
      
      ---
      // Path + Image files array:
      array (
        0 => 'events/2016-02-18-Sunny-Sport-Day/IMG_0336.jpg',
      )
      

      你弄糊涂了。首先你说你不想匹配那些带有参数的
      ,然后你马上给出你匹配它们的例子..我会把问题编辑得更清楚看看.你弄糊涂了。首先你说你不想匹配那些带有参数的
      ,然后你马上给出你匹配它们的例子。我将编辑这个问题以便更清楚地看一看。