2015-05-23 30 views
-1

我有一些像這樣的內容需要替換{{values}}。我如何在php中執行此操作。如何在PHP中的一個函數中替換所有字符串?如何簡化str中的替換php

$mail ="Dear {{ name }} , 
    Your warranty is going to expired in {{ count }} days. Please kindly renew it trhough your dashboard or drop a check. For further details call our sales dept at 1231212121212 
    Confirmation id: {{ confid }} 
    Warranty Period: {{ Effctivedate }} - {{ Expirydate }} 
    Property Address: {{ Address }}"; 

PHP

echo str_replace('{{ name }}','Test',$mail); 
+0

問題在哪裏? 'str_replace('** mixed $ search,mixed $ replace **,'mixed $ subject [,int&$ count])' – Rizier123

+0

其工作正常只爲我需要替換的名字'{{confid}}' {{Effctivedate}} - {{Expirydate}}'和'{{Address}}'在一個函數中,我該怎麼做? –

+0

[** RTM **](http://php.net/manual/en/function.str-replace.php) - > **混合** $搜索,**混合** $替換< - 意味着它也將'數組'作爲參數。 – Rizier123

回答

1

str_replace可以與陣列中使用。

echo str_replace(Array("{{ name }}", "{{ confid }}", ...), Array($name, $confid, ...), $mail); 

完成。

+1

感謝這正是我需要的:) –

+0

沒問題:-)如果你想:-),你可以投票答案並將其標記爲解決方案。 – Richard

0

試試這個。

 $mail ="Dear #NAME# , 
      Your warranty is going to expired in #count# days. Please kindly renew it trhough your dashboard or drop a check. For further details call our sales dept at 1231212121212 
      Confirmation id: #confid# 
      Warranty Period: #Effctivedate# - #Expirydate# 
      Property Address:#Address#"; 

    $mail= str_replace("#NAME#",$Name,$mail); 
    $mail= str_replace("#confid#",$confid,$mail); 
    $mail = str_replace("#Effctivedate#",$Effctivedate,$mail); 
    $mail = str_replace("#Expirydate#",$Expirydate,$mail); 

ecoh $mail;