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 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥35 关于shodan搜索网络摄像头的各种方法详解
  • ¥15 求PHDA糖尿病并发症数据集,有偿
  • ¥15 为什么AVL fire DVI 界面里面的response Editor project 中的Summary result 点不了
  • ¥20 中标麒麟系统V4.0,linux3.10.0的内核,3A4000处理器,如何安装英伟达或AMD的显卡驱动,
  • ¥20 求文心中文心理分析系统(TextMind)
  • ¥15 chipyard环境搭建问题