2012-01-03 127 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