2012-01-03 124 views
0

我只是想让我的头回合str_replace和大括号/大括号。 用下面这行代码,我知道输入{the_title}会取代数组$ some_runtime_generated_title,但第一个值是什么意思($ str_template)?使用str_replace替换子字符串

str_replace($str_template, '{the_title}', $some_runtime_generated_title); 

说我想要做以下........

$dog='lassy'; 
{dog} 
would output >> lassy 

我会怎么做,在PHP?

+0

您是否在为PHP寻找模板引擎(本着MustacheJS的精神)? – 2012-01-03 09:57:45

+0

这种问题很容易用[手册快速掌握](http://php.net/str_replace)来回答。它甚至有例子。我无法理解人们如何在没有手册的情况下编码。 – 2012-01-03 10:13:29

+0

我在来这里之前阅读手册,但是我不理解它,阅读和理解它们完全不同。 – 2012-01-03 10:25:30

回答

2

str_replace函数的一个简单的例子为占位更换会是什么样子:

$paramNames = array("{name}", "{year}", "{salutation}"); 
$paramValues = array("Iain Simpson", "2012", "Alloha"); 
$text = "{salutation}, {name}! Happy {year}!"; 
str_replace($paramNames, $paramValues, $text); 

$paramNames$paramValues阵列有相同数量的值。

一个更具体的目的功能将是:

/* Processes a text template by replacing {param}'s with corresponding values. A variation fo this function could accept a name of a file containing the template text. */ 
/* Parameters: */ 
/* template - a template text */ 
/* params - an assoc array (or map) of template parameter name=>value pairs */ 
function process_template($template, $params) { 
    $result = $template; 
    foreach ($params as $name => $value) { 
     // echo "Replacing {$name} with '$value'"; // echo can be used for debugging purposes 
     $result = str_replace("{$name}", $value, $result); 
    } 
    return $result; 
} 

用例:

$text = process_template("{salutation}, {name}! Happy {year}!", array(
    "name" => "Iain", "year" => 2012, "salutation" => "Alloha" 
)); 

下面是一个面向对象的方法的一个例子:

class TextTemplate { 
    private static $left = "{"; 
    private static $right = "}"; 
    private $template; 

    function __construct($template) { 
     $this->template = $template; 
    } 

    public function apply($params) { 
     $placeholders = array(); 
     $values = array(); 
     foreach($params as $name => $value) { 
      array_push($placeholders, self::$left . $name . self::$right); 
      array_push($values, $value); 
     } 
     $result = str_replace($placeholders, $values, $this->template); 
     return $result; 
    } 
} 

用例:

$template = new TextTemplate("{salutation}, {name}! Happy {year}!"); 
$text = $template->apply(array("name" => "Iain", "year" => 2012, "salutation" => "Alloha")); 
+0

啊,这样做更有意义,感谢您的帮助,稍后我会仔细研究一下,看看我能做些什么,它对电子邮件模板和事物来说非常有用,但我只是第一次阅读它时间昨天和大多数解释混淆。 – 2012-01-03 10:18:20

+0

我如何获得替换回声?我已尝试echo'{name}';但这只是给我{姓名},我也试过{姓名},但这给了我一个PHP错误。 – 2012-01-03 10:22:17

+0

@Iain,我不确定你想要实现什么,但是一个简单的变量echo可以作为'echo“{$ name}”;'完成。 – Vlad 2012-01-03 11:21:40

1

实际上你给自己的答案:

<?php 
$some_runtime_generated_title = 'generated title'; 
$str_template = 'This is the template in which you use {the_title}'; 
$parsed = str_replace('{the_title}', $some_runtime_generated_title, $str_template); 
echo $parsed; 
+0

我想''str_template'应该是最后一个参数。 – Vlad 2012-01-03 10:00:00

+0

对不起,我从另一篇文章中复制了这段代码,我没有自己写,这只是一个例子。这是否意味着$ str_template只是一个数组?所以第一部分是一个值的数组,第二部分是你想被替换的'关键字',第三部分是你希望它被替换的值,这是如何与第一个数组绑定的,是第一个数组的最后一个数组部分? – 2012-01-03 10:01:51

+0

如此:$ names {nameis} $ nameis ='frank'会回应坦率? – 2012-01-03 10:03:43