通过在文本中指定待替换的内容,如:
[{name}][{age}]格式可以自己定义,
大概过程:
在文本中定义需要替换的文本内容;
以键值对的方式 组织数据(数组);
用 file_get_contents() 读取整个文件的内容;
再用 strtr() 替换内容。
function make_content($file, array $vars = array()) { ???????$pairs = array(); ???????foreach ($vars as $name => $value) ???????{ ???????????$key = sprintf(‘[{%s}]‘, strtoupper($name)); // [{}] 格式自己定义 ???????????$pairs[$key] = (string)$value; ???????} ???????????????$template_content = ?file_get_contents($file); // 将文本读成字符串 ???????$result = strtr($template_content, $pairs); // 整个数组替换 ???????return $result; ???}php 通过 strtr 方法来替换文本中指定的内容
原文地址:https://www.cnblogs.com/tommy-huang/p/9406469.html