[英]php str_replace not working with special $ symbol
我有一個字符串:
$html = '${from_username} thinks ${to_username} is awesomelylylylly amazing';
我想替換
${from_username}
至
$data->from_username
我在做
str_replace("\${from_username}", $data->from_username, $html);
但是$ html是不受影響的
試試這段代碼:如果在字符串中使用雙引號,它將被視為PHP變量。 所以試試單引號。
str_replace('${from_username}', $data->from_username, $html);
因為它是星期五,我們都是沖動的蠢貨我的第一個答案:
使用
str_replace('${from_username}', $data->from_username, $html);
單引號不會在字符串中搜索變量,因此會忽略$
是不正確的。 正如評論中提到的,您的真正問題是,str_replace返回新字符串並且不會觸及傳遞的參數。 $將被忽略,因為你正確地逃脫了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.