PHP templating using custom code replacing using regex

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

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

thanks

4条回答
  •  抹茶落季
    2021-01-16 14:59

    This tested function has the (commented) regex you want:

    function process_data(&$text)
    {
        $re = '/# Reformat a string.
            \{       # Opening delimiter.
            (\$\w+)  # $1: game.
            \.       # Parts separated by dot.
            (\w+)    # $2: name.
            \.       # Parts separated by dot.
            (\w+)    # $3: fullname.
            \}       # Closing delimiter.
            /ix';
        $text = preg_replace($re, "$1['$2']['$3']", $text);
        return $text;
    }
    

提交回复
热议问题