Preg Match PHP Expression可从{{STRING}}中的文本捕获数组


Preg Match PHP Expression to capture an array from text within {{STRING}}

我有这个字符串

$url = offer?offer_id={{offer_category_id}}&item{{offer_title}}

有没有一种方法可以创建一个php数组,其中文本位于{{}}中,从而生成一个类似的数组

$array[0] = 'offer_category_id'
$array[1] = 'offer_title'

这就是我所拥有的,但它并没有像想要的那样工作

preg_match("/{{([^'"]*)'}}/", $url , $cols);

此代码将给出您的值:

$str = 'offer?offer_id={{offer_category_id}}&item{{offer_title}}';
if ( preg_match_all('~{'s*{([^}]*)}~i', $str, $m) )
   print_r ( $m[1] );

输出:

Array
(
    [0] => offer_category_id
    [1] => offer_title
)

使用preg_match_all:

preg_match_all("/{{([^'"}]+)'}}/", $url , $cols);

我认为应该是(您可能需要使用该标志来获得所需的订单):

preg_match_all("/{{([^}]+)'}}/", $url , $cols, PREG_SET_ORDER);