PHP preg_match函数变量提取

red*_*n76 1 php regex tags pattern-matching preg-match

假设我有一个像这样的字符串:

[$IMAGE[file_name|width|height]]
Run Code Online (Sandbox Code Playgroud)

如何匹配并获得2个变量

$tag = "IMAGE"
$param = "file_name|width|height"
Run Code Online (Sandbox Code Playgroud)

使用php preg_match函数?

Mic*_*ski 6

$string = '[$IMAGE[file_name|width|height]]';
// Matches only uppercase & underscore in the first component
// Matches lowercase, underscore, pipe in second component
$pattern = '/\[\$([A-Z_]+)\[([a-z_|]+)\]\]/';
preg_match($pattern, $string, $matches);

var_dump($matches);
array(3) {
  [0]=>
  string(32) "[$IMAGE[file_name|width|height]]"
  [1]=>
  string(5) "IMAGE"
  [2]=>
  string(22) "file_name|width|height"
}
Run Code Online (Sandbox Code Playgroud)