user3929758
user3929758

Reputation: 233

String replacement with multiple string

I have a message template where i need to replace the set of strings, for example

I have a message:

You have been invited by the [ADMIN_NAME] of the Organization [ORGANIZATION_NAME] on [DATETIME]

Needs to be replaced by ADMIN_NAME, ORGANIZATION_NAME, DATETIME

Upvotes: 0

Views: 45

Answers (2)

Shira
Shira

Reputation: 6570

Use 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;

Output:

You have been invited by the Some name of the Organization Some organization on some date

Upvotes: 2

Mostafa
Mostafa

Reputation: 909

<?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);

Add the template variables in the first array, and the values in the second array

And use the str_replace function after doing that

Upvotes: 0

Related Questions