我有一个字符串包含以下模式“[link:activate/$id/$test_code]”当模式 [link.....] 出现时,我需要从中获取单词 activate、$id 和 $test_code。
我还尝试通过使用分组来获取内部项目,但只能激活并且 $test_code 无法获取 $id。请帮我获取数组中的所有参数和动作名称。
下面是我的代码和输出
代码
function match_test()
{
$string = "Sample string contains [link:activate/\$id/\$test_code] again [link:anotheraction/\$key/\$second_param]]] also how the other ationc like [link:action] works";
$pattern = '/\[link:([a-z\_]+)(\/\$[a-z\_]+)+\]/i';
preg_match_all($pattern,$string,$matches);
print_r($matches);
}
输出
Array
(
[0] => Array
(
[0] => [link:activate/$id/$test_code]
[1] => [link:anotheraction/$key/$second_param]
)
[1] => Array
(
[0] => activate
[1] => anotheraction
)
[2] => Array
(
[0] => /$test_code
[1] => /$second_param
)
)