我在源中有此示例字符串: @include_plugin:PluginName param1=value1 param2=value2@
我想要的是找到来自来源的所有@include_plugin:*@
发生,以及PluginName
和每个paramN=valueN
的结果。
目前,我正在摆弄这样的东西(并尝试了许多变体):/@include_plugin:(.*'b){1}(.*'=.*){0,}@/
(使用此资源)。不幸的是,我似乎无法定义一种模式,它给了我想要的结果。有什么建议吗?
使用示例进行更新:假设我在 .tpl 文件中有这个字符串。 @include_plugin:BestSellers limit=5 fromCategory=123@
我希望它返回一个数组:
0 => BestSellers,
1 => limit=5 fromCategory=123
甚至更好(如果可能的话):
0 => BestSellers,
1 => limit=5,
2 => fromCategory=123
您可以通过 2 个步骤完成。首先使用正则表达式捕获该行,然后将参数分解为数组:
$subject = '@include_plugin:PluginName param1=value1 param2=value2@';
$pattern = '/@include_plugin:([a-z]+)( .*)?@/i';
preg_match($pattern, $subject, $matches);
$pluginName = $matches[1];
$pluginParams = isset($matches[2])?explode(' ', trim($matches[2])):array();
您可以使用此正则表达式:
/@include_plugin:([a-zA-Z0-9]+)(.*?)@/
插件名称位于第一个捕获组中,参数位于第二个捕获组中。请注意,参数(如果有)具有前导空格。
除非已知最大数量的参数,否则不可能编写正则表达式来提取到更好的情况下。
您可以通过先修剪前导空格和尾随空格,然后沿/'s+/
拆分来进行额外的处理。
我不确定您的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);
}
现在,您将在$plugins
中拥有以下内容:
Array
(
[0] => Array
(
[0] => PluginName
[1] => param1=value1
[2] => param2=value2
)
[1] => Array
(
[0] => BestSellers
[1] => limit=5
[2] => fromCategory=123
)
)
$string = "@include_plugin:PluginName1 param1=value1 param2=value2@ @include_plugin:PluginName2@";
preg_match_all('/@include_plugin:([a-zA-Z0-9]+)'s?([^@]+)?/', $string, $matches);
var_dump($matches);
这是你要找的吗?
array(3) {
[0]=>
array(2) {
[0]=>
string(55) "@include_plugin:PluginName1 param1=value1 param2=value2"
[1]=>
string(27) "@include_plugin:PluginName2"
}
[1]=>
array(2) {
[0]=>
string(11) "PluginName1"
[1]=>
string(11) "PluginName2"
}
[2]=>
array(2) {
[0]=>
string(27) "param1=value1 param2=value2"
[1]=>
string(0) ""
}
}
这个正则表达式将为您提供多个组,每个插件一个组。
((?<=@include_plugin:)(.+))