2016-08-08 500 views
0

我有,我需要更換一套字符串的郵件模板,例如字符串替換多個字符串

我有一個消息:

你被[ADMIN_NAME]的邀請組織 [ORGANIZATION_NAME]在[DATETIME]

需要由ADMIN_NAMEORGANIZATION_NAME代替,DATETIME

+0

只需創建一個數組並將值連接到字符串。 $ email = array(「adminName」=>「SO Admin」,「OrganizationName」=>「Stack Overflow」,「dateTime」=>「明天晚上10點」); $ string =「您已經被$ email ['dateTime']」的組織$ email ['OrganizationName']的$ email ['adminName']邀請了; –

回答

0
<?php 
$from = array('[ADMIN_NAME]','[ORGANIZATION_NAME]','[DATETIME]'); 
$to = array('ADMIN_NAME','ORGANIZATION_NAME','DATETIME'); 
$message = 'You have been invited by the [ADMIN_NAME] of the Organization [ORGANIZATION_NAME] on [DATETIME]'; 
echo str_replace($from,$to,$message); 

添加第一個數組中的模板變量,值第二陣列

IN和做後使用str_replace函數功能

2

使用strtr

$text = 'You have been invited by the [ADMIN_NAME] of the Organization [ORGANIZATION_NAME] on [DATETIME]'; 

$result = strtr($text, [ 
    '[ADMIN_NAME]' => 'Some name', 
    '[ORGANIZATION_NAME]' => 'Some organization', 
    '[DATETIME]' => 'some date', 
]); 

echo $result; 

輸出:

您已經被某些na邀請本組織的某些組織在某些日期某些組織我

+0

謝謝@ ShiraNai7 – user3929758