How to str_replace a section of PHP Code

故事扮演 提交于 2019-12-10 12:28:26

问题


$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


回答1:


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...




回答2:


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



来源:https://stackoverflow.com/questions/2459210/how-to-str-replace-a-section-of-php-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!