php - Regex extract variables from [shortcode] -


after migrating content wordpress drupal, i've got som shortcodes need convert:

string content:

irrelevant tekst... [sublimevideo 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"] ..more irrelevant text.

i need find variables within shortcode [sublimevideo ...] , turn array:

array (     class => "sublime"     poster => "http://video.host.com/_previews/600x450/sbx-60025-00-da-fmt.png"     src1 => "http://video.host.com/_video/h.264/lo/sbx-60025-00-da-fmt.m4v"     src2 => "(hd)http://video.host.com/_video/h.264/hi/sbx-60025-00-da-fmt.m4v"     width => "560"     height => "315" ) 

and preferably handle multiple instances of shortcode.

i guess can done preg_match_all() i've had no luck.

this give want.

$data = 'irrelevant tekst... [sublimevideo 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"] ..more irrelevant text.';  $dat = array(); preg_match("/\[sublimevideo (.+?)\]/", $data, $dat);  $dat = array_pop($dat); $dat= explode(" ", $dat); $params = array(); foreach ($dat $d){     list($opt, $val) = explode("=", $d);     $params[$opt] = trim($val, '"'); }  print_r($params); 

in anticipation of next challenge face processing short codes can use preg_replace_callback replace short tag data it's resultant markup.

$data = 'irrelevant tekst... [sublimevideo 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"] ..more irrelevant text.';  function processshortcode($matches){     // parse out arguments     $dat= explode(" ", $matches[2]);     $params = array();     foreach ($dat $d){         list($opt, $val) = explode("=", $d);         $params[$opt] = trim($val, '"');     }     switch($matches[1]){         case "sublimevideo":             // here want return resultant markup shorttag call.              return print_r($params, true);             }  } $data = preg_replace_callback("/\[(\w+) (.+?)]/", "processshortcode", $data); echo $data; 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -