doutang7383 2017-07-14 12:34
浏览 233
已采纳

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

I have several texts with custom macro tags in it. I want to parse the content of these tags but I'd like to treat the ones with parameters in it differently.

I need to construct valid URLs out of these bracket contents.

Example:

This is my text:

{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}

So I need to extract the path parts of the string between the {gallery} tags but I don't want to match those with parameters such as "single=IMG_0336.jpg,salign=left" because these are treated separately.

I need something which does the following:

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

becomes

first output: events/2016-02-18-Sunny-Sport-Day

second output: IMG_0336.jpg

and

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

becomes

events/2016-02-18-Sunny-Sport-Day

Tried the following regex:

/\{gallery\}(.*?)(?!single=)\{\/gallery\}/

But it allways matches the whole string including the single parameter.

To get the content of the single parameter I tried the following:

/,single=(.*?),/

This works fine for the single paramter only but I don't know how to get everything together.

Conclusion:

In a PHP environment I would like to have as output two arrays. The first consists only of the folders:

  • events/2016-02-18-Sunny-Sport-Day
  • team/members

and a second array which consists of the single file paths:

  • events/2016-02-18-Sunny-Sport-Dayist/IMG_0336.jpg
  • 写回答

2条回答 默认 最新

  • douxian9706 2017-07-19 04:00
    关注

    This method will extract your desired substrings and prepare the output data as you have requested: Pattern Demo

    PHP Code: (Demo)

    $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}";
    preg_match_all('@\{gallery\}([^,]*?)(?:,single=([^,{]+).*?)?\{/gallery\}@',$str,$out);
    
    // Matches array:
    var_export($out);
    
    echo "
    
    ---
    
    ";
    
    // Folders only array:
    var_export(array_filter(array_slice($out,1)[0],'strlen'));
    
    echo "
    
    ---
    
    ";
    
    // Path + Image files array:
    foreach($out[2] as $i=>$v){
        if($v){
            $result[]="{$out[1][$i]}/$v";
        }
    }
    var_export($result);\
    

    Output:

    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',
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊