使用file_get_contents后,PHP替换字符串 [英] PHP replace string after using file_get_contents
本文介绍了使用file_get_contents后,PHP替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想替换通过 file_get_contents
这是我的代码:
<?
$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);
?>
我希望找到SAD的所有模式(区分大小写)并将其替换为HAPPY.由于某些原因,如果我使用 file_get_contents ,它似乎无法正常工作.
I am hoping to find all the patters of SAD (case sensitive) and replace them with HAPPY. For some reason if I use file_get_contents it doesn't seem to be working.
谢谢
更新日期:2013年1月22日
实际上,对不起,当我加上$时,它不起作用.这对于我的代码不是必需的.我可以解决,但这在下面不起作用:
Actually, sorry Correction when I add the $ it does not work. Its not necessary for my code. I can do a work around but this does not work below:
$message = str_replace("$SAD", "HAPPY", $message); /// does not work. Not sure why
$message = str_replace("SAD", "HAPPY", $message); /// without the $ it does work.
推荐答案
$message = str_replace("$SAD", "HAPPY", $message);
需要是:
$message = str_replace('$SAD', "HAPPY", $message);
否则,PHP将其解释为变量$SAD
.请参阅此帖子,以获取有关单引号和双引号之间的区别.
Otherwise PHP will interpret it as the variable $SAD
. See this post for an explanation on the difference between single and double quotes.
这篇关于使用file_get_contents后,PHP替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文