Pradeep shyam
Pradeep shyam

Reputation: 1292

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

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

Upvotes: 3

Views: 2590

Answers (3)

anubhava
anubhava

Reputation: 784998

This will work:

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

OUTPUT:

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

Upvotes: 8

Andrew Cheong
Andrew Cheong

Reputation: 30273

preg_match_all('/\{\{([^}]+)\}\}/', $str, $matches);
$array = $matches[1];

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 157947

Use this:

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

Output:

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

Upvotes: 1

Related Questions