Regex to match “{number}”

前端 未结 2 1885
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 01:10

I need to replace \"{Z}\" with \"test(Z)\" where Z is always an unsigned integer using PHP and regular expressions (unless there\'s a faster way?).

$code=\'{         


        
2条回答
  •  臣服心动
    2021-01-06 01:18

    $code = preg_replace('/\{(\d+)\}/', 'test($1)', $code);
    

    In my experience, preg_replace is much faster than any method of doing replacements using str_replace or strtr.

提交回复
热议问题