用于获取双大括号“{{ }}”内的值的正则表达式


Regular expression to get the value inside double curly braces "{{ }}"

PHP 中的正则表达式,用于获取数组中带有"{{ }}"引用的文本。

例如:

$str = "This is sample content with a dynamic value {{value1}} and also have more dynamic values {{value2}}, {{value3}}";

需要输出如下数组,

array(value1,value2,value3);

这将起作用:

$str = "This is sample content with a dynamic value {{value1}} and also have more dynamic values {{value2}}, {{ value3 }}";
if (preg_match_all("~'{'{'s*(.*?)'s*'}'}~", $str, $arr))
   var_dump($arr[1]);

输出:

array(3) {
  [0]=>
  string(6) "value1"
  [1]=>
  string(6) "value2"
  [2]=>
  string(6) "value3"
}

使用这个:

preg_match_all('~'{'{(.*?)'}'}~', $string, $matches);
var_dump($matches[1]);

输出:

array(3) {
  [0] =>
  string(6) "value1"
  [1] =>
  string(6) "value2"
  [2] =>
  string(6) "value3"
}
preg_match_all('/'{'{([^}]+)'}'}/', $str, $matches);
$array = $matches[1];