2011-07-01 51 views
0

我有一個輸出php啓用的模板。但是我無法讓str_replace工作。見下面的代碼。如果我只是把{title}沒有任何PHP然後它的作品。但是,當我包含php並將{title}放入str_replace函數中時,我什麼也沒有顯示。我試圖用簡單的回聲「測試」PHP;它的工作。它只是不分析{title}或其他字段標籤。expressionEngine php解析str_replace不起作用

{exp:channel:entries channel="one" url_title="{segment_2}"} 
    <?php 
    echo str_replace('old', 'new', '{title}'); 
    ?> 
{/exp:channel:entries} 

回答

2

這可能聽起來很荒謬,但是您是否嘗試從{exp:channel:entries}標記中刪除url_title參數以確保代碼和邏輯的其餘部分正常工作?

應下面的代碼輸出「舊的測試」(不帶引號)的條目標題:,這應該

{exp:channel:entries channel="channel_name" entry_id="xx" dynamic="off"} 
    {title} 
{/exp:channel:entries} 

鑑於「老測試」相同的條目標題輸出:再次「新的測試」( ,不帶引號):

{exp:channel:entries channel="channel_name" entry_id="xx" dynamic="off"} 
    <?php 
     echo str_replace('old', 'new', '{title}'); 
    ?> 
{/exp:channel:entries} 

這是與支持PHP,並設置爲輸出一個新的空白模板測試成功。

Screenshot

+0

@thanks @rjb,我應該使用'segment_3'而不是'segment_2'。它現在工作,但是,如果我有以下str_replace數組的話,它不起作用。 PHP似乎不認可標題。 '$ searchArray = array(「on」,「two」,「thre」,「fou」); $ replaceArray = array(「one」,「two」,「three」,「four」); $ intoString =「{title}」; echo str_replace($ searchArray,$ replaceArray,$ intoString);'爲什麼'{title}'在這裏不被識別。 – Pinkie

+0

以下代碼適用於我:[Screenshot](http://i.stack.imgur.com/9odhR.png)或[Pastie](http://pastie.org/pastes/2175892/text) – rjb

0

您可以嘗試嵌入帶有PHP的模板,並以此方式發送變量。輸入/輸出解析對於某些元素有時並不重要。

+0

這不是一個解決方案。知道我在說什麼的唯一方法就是親自嘗試一下。 – Pinkie