Pap*_*eau 1 php preg-replace file-get-contents str-replace
嗨,我希望替换我通过file_get_contents加载的html电子邮件中的单词
这是我的代码:
<?
$message = file_get_contents("http://www.MYwebsiteExample.com/EmailConfirmation.php");
$message = preg_replace('/SAD/', "HAPPY", $message);
// Also tried this below and it does not work either
$message = str_replace('/SAD/', "HAPPY", $message);
?>
Run Code Online (Sandbox Code Playgroud)
我希望找到SAD的所有模式(区分大小写)并用HAPPY替换它们.出于某种原因,如果我使用file_get_contents它似乎没有工作.
谢谢
更新:2013年1月22日
实际上,抱歉当我添加$时更正不起作用.它对我的代码不是必需的.我可以做一个解决方法,但这不起作用:
$message = str_replace("$SAD", "HAPPY", $message); /// does not work. Not sure why
$message = str_replace("SAD", "HAPPY", $message); /// without the $ it does work.
Run Code Online (Sandbox Code Playgroud)
$message = str_replace("$SAD", "HAPPY", $message);
Run Code Online (Sandbox Code Playgroud)
需要是:
$message = str_replace('$SAD', "HAPPY", $message);
Run Code Online (Sandbox Code Playgroud)
否则PHP会将其解释为变量$SAD.有关单引号和双引号之间差异的说明,请参阅此文章.