如何str_replace PHP代码的一部分

发布于 2024-08-25 14:08:13 字数 378 浏览 2 评论 0原文

$embedCode = <<<EOF
getApplicationContent('video','player',array('id' => $iFileId, 'user' => $this->iViewer, 'password' => clear_xss($_COOKIE['memberPassword'])),true)
EOF;
$name = str_replace($embedCode,"test",$content);

我正在尝试用另一段代码替换一段代码。我可以使用较小的字符串来完成此操作,但是一旦将较大的字符串添加到 $embedCode 中,它就会抛出“意外的 T_ENCAPSED_AND_WHITESPACE”错误

$embedCode = <<<EOF
getApplicationContent('video','player',array('id' => $iFileId, 'user' => $this->iViewer, 'password' => clear_xss($_COOKIE['memberPassword'])),true)
EOF;
$name = str_replace($embedCode,"test",$content);

I'm trying to replace a section of code with another piece of code. I can do it with smaller strings but once I added the larger strings to $embedCode, it throw an "unexpected T_ENCAPSED_AND_WHITESPACE" error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-09-01 14:08:13

你应该使用 \$ 转义 $

$embedCode = <<<EOF
    getApplicationContent('video','player',array('id' => \$iFileId, 'user' => \$this->iViewer, 'password' => clear_xss(\$_COOKIE['memberPassword'])),true)
EOF;

如果你的目标是使用变量名称,如果你想使用变量的真实值,那么问题出在 $这->iViewer...

you should unescape the $ using \$

$embedCode = <<<EOF
    getApplicationContent('video','player',array('id' => \$iFileId, 'user' => \$this->iViewer, 'password' => clear_xss(\$_COOKIE['memberPassword'])),true)
EOF;

IF your objective is to use the vars name, if you want to use the real value of the variables, then the problem is in $this->iViewer...

山田美奈子 2024-09-01 14:08:13

无论如何,删除 $_COOKIE 附近的 memberPassword 周围的 '

似乎您正在寻找不解释内部变量的语言构造 - 所以您必须使用不 HEREDOC 语法 - 但常规字符串定义限制为 '

$sample = 'qwe $asd zxc';

或用 \ 转义 $,如 Marcx 在下面建议的那样

remove ' around the memberPassword near the $_COOKIE

anyway seems you're looking for language construction that not interprets variable inside - so then you have to use not HEREDOC syntax - but regular string definition limited with '

$sample = 'qwe $asd zxc';

or escape $ with \ as Marcx propose below

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文