用于在双花括号"{{}}"中获取值的正则表达式; [英] Regular expression to get the value inside double curly braces "{{ }}"

查看:437
本文介绍了用于在双花括号"{{}}"中获取值的正则表达式;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如:

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

推荐答案

这将起作用:

$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"
}

这篇关于用于在双花括号"{{}}"中获取值的正则表达式;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆