3

Regular expression in PHP to fetch the text quoted inside with "{{ }}" in an array.

For eg:

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

Need output as like below array,

array(value1,value2,value3);
4

3 回答 3

8

这将起作用:

$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"
}
于 2013-10-11T11:24:37.310 回答
1

用这个:

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

输出:

array(3) {
  [0] =>
  string(6) "value1"
  [1] =>
  string(6) "value2"
  [2] =>
  string(6) "value3"
}
于 2013-10-11T11:24:10.073 回答
0
preg_match_all('/\{\{([^}]+)\}\}/', $str, $matches);
$array = $matches[1];
于 2013-10-11T11:24:58.063 回答