问题描述
将一些内容从 WordPress 迁移到 Drupal 后,我有一些短代码需要转换:
After migrating some content from WordPress to Drupal, I've got som shortcodes that I need to convert:
字符串内容:
无关的测试...[崇高视频类=崇高"海报="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"宽度="560" 高度="315"]..更多不相关的文字.
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.
我需要在短代码 [sublimevideo ...] 中找到所有变量并将其转换为数组:
I need to find all variables within the shortcode [sublimevideo ...] and turn it into an 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 the shortcode.
我想它可以用 preg_match_all() 来完成,但我没有运气.
I guess it can be done with preg_match_all() but I've had no luck.
推荐答案
这会给你你想要的.
$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 as $d){
list($opt, $val) = explode("=", $d);
$params[$opt] = trim($val, '"');
}
print_r($params);
为了应对处理短代码的下一个挑战,您可以使用 preg_replace_callback 将短标签数据替换为其结果标记.
In anticipation of the next challenge you will face with processing short codes you can use preg_replace_callback to replace the short tag data with 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 the arguments
$dat= explode(" ", $matches[2]);
$params = array();
foreach ($dat as $d){
list($opt, $val) = explode("=", $d);
$params[$opt] = trim($val, '"');
}
switch($matches[1]){
case "sublimevideo":
// here is where you would want to return the resultant markup from the shorttag call.
return print_r($params, true);
}
}
$data = preg_replace_callback("/[(w+) (.+?)]/", "processShortCode", $data);
echo $data;
这篇关于正则表达式从 [简码] 中提取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!