配列内の "{{ }}" で囲まれたテキストを取得する 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];