php - 无法替换字符串中的变量名
问题描述
我想发送一条系统消息,用他的名字称呼用户。消息存储在 .txt 文件中,如下所示:
你好 $user-> 名字
登录链接:something.something/user/id
在 userController(发送消息的地方)中,我现在尝试用实际的 $user->firstname 替换 $user->firstname:
$output = file_get_contents(Yii::$app->basePath."message.txt");
$user = $this->findModel($id); //this is tested and works
$output = str_replace("$user->firstname", $user->firstname, $output);
但是,我在此之后的输出仍然与文本文件中的完全相同。我究竟做错了什么?
解决方案
我认为这可能就像在str_replace
通话中使用单引号一样简单:
$output = str_replace('$user->firstname', $user->firstname, $output);
当你使用双引号时,PHP 在调用str_replace
.
有关更多信息,请参阅https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing。
推荐阅读
- python - 下载 csv 文件并转换为 JSON
- angular - Angular - 未验证动态 FormArray 控件
- latex - 为什么长表在乳胶中没有转到下一页
- c - Snmpset 不适用于 mib2c 生成的代理
- angular - 如何在角度分量的ngrx动作中仅获得第一个值?
- javascript - 如何在谷歌中使用密码锁定网站?
- python - 在 MacOS 上设置 Intel One API 框架 - 当我执行“source /opt/intel/oneapi/setvars.sh”时如何减少 conda 的加载时间
- vsto - 无法添加自定义用户属性,即使它不存在
- bios - 有没有办法修改计算机的 DHCP 发现数据包中的字段?
- android - 构建被配置为首选设置存储库而不是项目存储库,但存储库“maven”是由构建文件“build.gradle”添加的