正则表达式可在一场比赛中重复比赛 - php

我在源代码中有以下示例字符串:
@include_plugin:PluginName param1=value1 param2=value2@

我想要的是从源中查找所有出现的@include_plugin:*@,并给出PluginName和每个paramN=valueN的结果。

此时,我正在摆弄这样的东西(并尝试了许多变体):/@include_plugin:(.*\b){1}(.*\=.*){0,}@/(使用此resource)。不幸的是,我似乎无法定义一种模式,该模式可以为我提供想要的结果。有什么建议?

更新示例:
说我在.tpl文件中有此字符串。 @include_plugin:BestSellers limit=5 fromCategory=123@

我希望它返回一个数组:

0 => BestSellers, 
1 => limit=5 fromCategory=123 

甚至更好(如果可能):

0 => BestSellers, 
1 => limit=5, 
2 => fromCategory=123

参考方案

您可以分两步完成。首先使用正则表达式捕获行,然后将参数分解为数组:

$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();

php preg_replace两个或更多空格 - php

我正在寻找替换多个空格字符的实例。我最初的搜索似乎都集中在使用/s,但这包括换行符和其他空格我认为应该接近吗?用一个空格替换两个或更多实例空格" "preg_replace('/ {2,}/', ' ', $string); 参考方案 尝试一下:preg_replace('/\s\s+/&…

PHP preg_match直到双行中断 - php

我在mysql字段中有此数据:First text text text text text text text text text text text text text text text text text text text text Second text text text text text text text text text text te…

preg_match不捕获内容 - php

我的preg_match有什么问题?preg_match('numVar("XYZ-(.*)");',$var,$results); 我想从这里获取所有内容:numVar(“ XYZ-CONTENT”);感谢您的任何帮助! 参考方案 我认为这是PHP?如果是这样,则您的代码存在三个问题。PHP的PCRE函数要求使用分隔…

PHP:Preg_replace,单词边界和非单词字符 - php

我需要替换文本中以井号(#)开头的单词。好吧,我知道如何替换整个单词。preg_replace("/\b".$variable."\b/", $value, $text);由于该\ b修饰符仅接受单词字符,因此不会替换包含井号的单词。我有这个html,其中包含#companyName类型的变量,我将其替换为一个值。 参…

PHP preg_replace删除字符串中的第一个HTML元素 - php

我想在PHP中删除html字符串(始终是一个段落)的整个第一个元素。我目前的方法是使用:$passage = preg_replace('/.*?\b'.'</p>'.'\b/s', '', $passage, 1); 由于</p>中的特殊字符,此方法不起…