Dan_J
Dan_J

Reputation: 165

str_replace function replace placeholders

I've got the following string which has been loaded from a database into $message.

Hi,\n\nYou have a new voicemail message from ${VM_CIDNAME} (${VM_CIDNUM}), on ${VM_DATE}.\n\nThank you for using 123 Solutions.

I'm trying to str_replace the placeholders in that string with real data, but since PHP is interpreting ${VM_CIDNAME}, it isn't performing a replacement.

$message = str_replace("${VM_CIDNUM}", $CallerID , $message);

How can I replace the placeholders?

Note: Changing the format of the placeholders is not an option as it's hard coded into third party software which I cannot change.

I also need to str_replace the "\n" characters with newlines.

Many thanks

Upvotes: 1

Views: 195

Answers (3)

Oscar Zarrus
Oscar Zarrus

Reputation: 790

Simple.

/* SET string variables before set $message */

$VM_CIDNAME = $CallerNAME; /* Your CallerNAME variable */
$VM_CIDNUM = $CallerID; /* Your CallerID variable */
$VM_DATE = $CallerDATE; /* Your CallerDATE variable */

/* then */
 $message = "Hi,\n\nYou have a new voicemail message from ${VM_CIDNAME} (${VM_CIDNUM}), on ${VM_DATE}.\n\nThank you for using 123 Solutions.";
 $message = nl2br($message);

UPDATE

$VM_CIDNAME = 'John';
$VM_CIDNUM = '1234';
$VM_DATE = '2017-01-01';
$dbRes = $mailboxinfo['emailbody'];
$ev = '$message = "' . $dbRes . '";';
eval($ev);
$message = nl2br($message);

without nothing to replace

Upvotes: 0

Sooraj
Sooraj

Reputation: 432

You must use single quotes(')

$message = str_replace('${VM_CIDNUM}', $CallerID , $message);

Upvotes: 1

PravinS
PravinS

Reputation: 2594

Try using single quote(')

$message = str_replace('${VM_CIDNUM}', $CallerID , $message);

and to replace "\n" use nl2br() PHP function as

$message = nl2br($message);

Upvotes: 2

Related Questions