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 为啥快手广告联盟的广告这么难出来
  • ¥15 k8s集群重启后,kubelet一直报systemctl restart kubelet.service "Failed to delete cgroup paths"
  • ¥15 微信小程序构建工具类发布到npm,install后报错
  • ¥15 amesim液压仿真问题
  • ¥15 python解析json文件时报错
  • ¥15 MATLAB优化设计
  • ¥15 ANN预测模型 200大米
  • ¥20 优秀学生的问题 oj java
  • ¥15 scratch-gui 如何集成进 react native
  • ¥15 把全局悬浮窗改为局内悬浮窗