我不确定您PluginName
可以包含的字符集或参数/值,但如果它们受到限制,您可以使用以下正则表达式:
/@include_plugin:((?:\w+)(?:\s+[a-zA-Z0-9]+=[a-zA-Z0-9]+)*)@/
这将捕获插件名称,后跟任何字母数字参数列表及其值。可以通过以下方式看到输出:
<?
$str = '@include_plugin:PluginName param1=value1 param2=value2@
@include_plugin:BestSellers limit=5 fromCategory=123@';
$regex = '/@include_plugin:((?:\w+)(?:\s+[a-zA-Z0-9]+=[a-zA-Z0-9]+)*)@/';
$matches = array();
preg_match_all($regex, $str, $matches);
print_r($matches);
?>
这将输出:
Array
(
[0] => Array
(
[0] => @include_plugin:PluginName param1=value1 param2=value2@
[1] => @include_plugin:BestSellers limit=5 fromCategory=123@
)
[1] => Array
(
[0] => PluginName param1=value1 param2=value2
[1] => BestSellers limit=5 fromCategory=123
)
)
要以您需要的格式获取数组,您可以使用以下命令遍历结果:
$plugins = array();
foreach ($matches[1] as $match) {
$plugins[] = explode(' ', $match);
}
And now you'll have the following in $plugins
:
Array
(
[0] => Array
(
[0] => PluginName
[1] => param1=value1
[2] => param2=value2
)
[1] => Array
(
[0] => BestSellers
[1] => limit=5
[2] => fromCategory=123
)
)