replace tags with PHP variables using preg_match

前端 未结 4 1665
误落风尘
误落风尘 2020-12-22 03:13

{$thisname} should be converted to .

{thisname} should be converted to

4条回答
  •  执笔经年
    2020-12-22 03:57

    $tpl = 'Name: {$name}, Surname: {surname}, City: {$person.address.city}';
    
    function tpl2php($m){
        $var =  $m[1];
        if(strpos($var,'.')){
            $varArr = explode('.',$var);
            $var = $varArr[0];
            for($i=1;$i';
    }
    
    $php = preg_replace_callback('/{\$?([_a-z][\w\.]+[\w])}/iS','tpl2php',$tpl);
    // Name: , Surname: , City: 
    

提交回复
热议问题