正则表达式在PHP中用数组中的“{{}}”获取引用的文本。正则表达式获取双花括号内的值“{{}}”
对于例如:
$str = "This is sample content with a dynamic value {{value1}} and also have more dynamic values {{value2}}, {{value3}}";
需要输出作为像下面阵列,
array(value1,value2,value3);
正则表达式在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]);
OUTPUT:
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];
你尝试过什么? –
我不是很擅长正则表达式,并尝试通过引用此:http://php.net/manual/en/function.preg-match.php – shyammtp