1

Hi I am looking to replace words in an html email I am loading via file_get_contents

Here is my code:

<?

$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);

?>

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.

Thanks

UPDATE: Jan 22, 2013

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.
Papa De Beau
  • 3,744
  • 18
  • 79
  • 137
  • What does "it doesn't seem to be working" mean? Are you getting errors? Unexpected results? – Mike Brant Jan 22 '13 at 19:07
  • 4
    what does `var_dump($message);` output? You might not even be getting any results. [allow_url_fopen](http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen) might be set to off. – kittycat Jan 22 '13 at 19:09
  • ok I made an update. It won't work with the $ and not sure why. It's not necessary for my code but just curious as to why not. – Papa De Beau Jan 22 '13 at 19:14
  • The file you're pulling in is a HTML file, and there might actually be *no literal "SAD"* in it anywhere. There might be, for example, `SAD`. If this is the case, you would need a different regexp, or better still, use DOM manipulation. – LSerni Jan 22 '13 at 19:14
  • `$SAD` is a variable called `$SAD` (undefined in your code), not the text `"SAD"` -- that's what in your second line. – Nate Cook Jan 22 '13 at 19:14
  • 1
    Also, you had better use explicit PHP tags - ` – LSerni Jan 22 '13 at 19:15

2 Answers2

9
$message = str_replace("$SAD", "HAPPY", $message);

needs to be:

$message = str_replace('$SAD', "HAPPY", $message);

Otherwise PHP will interpret it as the variable $SAD. See this post for an explanation on the difference between single and double quotes.

Community
  • 1
  • 1
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • Or maybe just `$message = str_replace('SAD', 'HAPPY', $message);`? Not super clear why that dollar sign is in there... – Nate Cook Jan 22 '13 at 19:15
  • YOU GOT IT. That worked. I will mark the answer correct when it allows me to. You rock. Thank you so much! – Papa De Beau Jan 22 '13 at 19:17
  • @lserni I think OP is trying to replace template variables in a file which happen to have the format of a PHP variable. Should be using a template variable like `%{SAD}` for example instead. – kittycat Jan 22 '13 at 19:19
  • 1
    Thank you S&B! I mean @cryptic. S&B = Smart & Beautiful. :) Much appreciated! – Papa De Beau Jan 22 '13 at 19:21
3

You shouldn't use regular expressions for this; it's simple string replacement:

$message = strtr($message, array(
    '$SAD' => 'HAPPY',
));

Btw, if you use "$SAD" for the search string, PHP will try to evaluate a variable called $SAD, which doesn't exist and will throw a notice if your error_reporting is configured to show it.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • You're answer worked as well @Jack. Thank you. What is the downfall of using a regular expression as opposed to the string replacement? – Papa De Beau Jan 22 '13 at 19:34
  • 2
    @PapaDeBeau Regular expressions are more powerful but take up more resources and can be slower in execution. Besides that, programming is all about developing a sense of finesse for making something work as simple as possible :) – Ja͢ck Jan 22 '13 at 19:43
  • @jack I am inclined to use the second part of your statement as a claim for my company. https://renoi.de/en :D – Lightningsoul Jun 05 '18 at 13:38
  • @Lightningsoul go for it :) – Ja͢ck Jun 05 '18 at 14:00