zoukankan      html  css  js  c++  java
  • php 通过 strtr 方法来替换文本中指定的内容

    通过在文本中指定待替换的内容,如:

    [{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;
        }
  • 相关阅读:
    The first appliaction for "Hello World!"
    zone
    learn to study
    深入理解 Angular 2 变化监测和 ngZone
    看看吧
    生命周期钩子
    一个简单的todo
    依赖注入
    @Output()
    @Input
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/9406469.html
Copyright © 2011-2022 走看看