我有一个消息模板,我需要替换字符串集,例如
我有一条消息:
您已受到本组织[ADMIN_NAME]的邀请 [DATETIME]
上的[ORGANIZATION_NAME]
需要替换为ADMIN_NAME
,ORGANIZATION_NAME
,DATETIME
答案 0 :(得分: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;
输出:
本组织的某些名称邀请了某个组织某个日期
答案 1 :(得分: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);
在第一个数组中添加模板变量,在第二个数组中添加值
执行该操作后使用str_replace函数