[英]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 但是$ html是不受影响的
try this code: If you use double quote inside the string, It'll be treated as PHP variable. 试试这段代码:如果在字符串中使用双引号,它将被视为PHP变量。 So try ith single quote.
所以试试单引号。
str_replace('${from_username}', $data->from_username, $html);
And because it is friday and we are all impulsive morons my first answer: 因为它是星期五,我们都是冲动的蠢货我的第一个答案:
Use
str_replace('${from_username}', $data->from_username, $html);
使用
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.
正如评论中提到的,您的真正问题是,str_replace返回新字符串并且不会触及传递的参数。 And the $ will be ignored because you escaped it correctly.
$将被忽略,因为你正确地逃脱了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.