PHP templating using custom code replacing using regex

前端 未结 4 846
暖寄归人
暖寄归人 2021-01-16 14:41

How can I convert {$game.name.fullname} to $game[\'name\'][\'fullname\'] using regex.

thanks

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 15:13

    preg_replace(
        '/{\$([^.{}]*)\.([^.{}]*)\.([^.{}]*)}/',
        '$$1[\'$2\'][\'$3\']',
        '{$game.name.fullname}');
    

    Edit: Edited according to ridgerunner's comment. Now matches anything between dots and matches multiple strings.

提交回复
热议问题