PHP- 正则表达式

2018-03-23 17:00:24 阅读:1 编辑
$string = 'hello world {{code}},{{hello}}';
    $data = ["code"=>"code2","hello"=>"hello2"];
    preg_match_all ('/{{.*?}}/', $string, $matches, PREG_OFFSET_CAPTURE);
    var_dump ($matches);
    foreach ($matches [0] as $item)
    {$lists [] = [$item [0] => $data [trim ($item [0],"{}")]];
        $string = str_replace ($item [0],$data [trim ($item [0],"{}")],$string);
    }
    var_dump ($lists);
    var_dump ($string);