user3023421
user3023421

Reputation: 363

php str_replace not working with special $ symbol

I have a string:

$html = '${from_username} thinks ${to_username} is awesomelylylylly amazing';

I am trying to replace

${from_username}

to

$data->from_username

I am doing

str_replace("\${from_username}", $data->from_username,  $html);

however $html is untouched

Upvotes: 0

Views: 103

Answers (2)

th3falc0n
th3falc0n

Reputation: 1427

And because it is friday and we are all impulsive morons my first answer:

Use str_replace('${from_username}', $data->from_username, $html);

Single quotation marks won't search the string for variables and and thus will ignore the $

is incorrect. As mentioned in the comments your real problem is, that str_replace returns the new string and wont touch the passed arguments. And the $ will be ignored because you escaped it correctly.

Upvotes: 1

Kumar V
Kumar V

Reputation: 8838

try this code: If you use double quote inside the string, It'll be treated as PHP variable. So try ith single quote.

str_replace('${from_username}', $data->from_username,  $html);

Upvotes: 1

Related Questions