且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP 用数组中的值替换字符串

更新时间:2023-01-30 13:01:08

你可以试试这个吗,

    $string ="Hello <%First Name%> <%Last Name%> welcome";
    preg_match_all('~<%(.*?)%>~s',$string,$datas);
    $Array = array('0' => array ('First Name' => 'John', 'Last Name' => 'Smith' ));
    $Html =$string;
    foreach($datas[1] as $value){           
        $Html =str_replace($value, $Array[0][$value], $Html);
    }
    echo str_replace(array("<%","%>"),'',$Html);