Regex extract variables from [shortcode]

后端 未结 4 1731
礼貌的吻别
礼貌的吻别 2020-12-18 08:50

After migrating some content from WordPress to Drupal, I\'ve got som shortcodes that I need to convert:

String content:

Irre

4条回答
  •  星月不相逢
    2020-12-18 09:39

    You could use the following RegEx to match the variables:

    $regex = '/(\w+)\s*=\s*"(.*?)"/';
    

    I would suggest to first match the sublimevideo shortcode and get that into a string with the following RegEx:

    $pattern = '/\[sublimevideo(.*?)\]/';
    

    To get the correct array keys I used this code:

    // $string is string content you specified
    preg_match_all($regex, $string, $matches);
    
    $sublimevideo = array();
    for ($i = 0; $i < count($matches[1]); $i++)
        $sublimevideo[$matches[1][$i]] = $matches[2][$i];
    

    This returns the following array: (the one that you've requested)

    Array
    (
        [class] => sublime
        [poster] => http://video.host.com/_previews/600x450/sbx-60025-00-da-ANA.png
        [src1] => http://video.host.com/_video/H.264/LO/sbx-60025-00-da-ANA.m4v
        [src2] => (hd)http://video.host.com/_video/H.264/HI/sbx-60025-00-da-ANA.m4v
        [width] => 560
        [height] => 315
    )
    

提交回复
热议问题