// 字符串模板,懒得构造字符串,这里直接读取文件来模拟 $str = file_get_contents( 'tpl.php' ); // 首尾添加php脚本标签,使所有原样输出的html文本处于php脚本标签的中间 $str = {$str}; // 用echo语句替换原样输出的html文本 $str = preg_replace_callback( '/\?>([\s\s]+?)'; }, $str ); // 去掉php脚本标签并执行标签里面的代码 eval( str_replace( array ( '' ), '', $str ) );