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.
6
  • What does "it doesn't seem to be working" mean? Are you getting errors? Unexpected results?
    – Mike Brant
    Jan 22, 2013 at 19:07
  • 4
    what does var_dump($message); output? You might not even be getting any results. allow_url_fopen might be set to off.
    – kittycat
    Jan 22, 2013 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. Jan 22, 2013 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, <span class="firstletter">S</span>AD. If this is the case, you would need a different regexp, or better still, use DOM manipulation.
    – LSerni
    Jan 22, 2013 at 19:14
  • 1
    Also, you had better use explicit PHP tags - <?php, not <?. Just to pick up a good habit ;-)
    – LSerni
    Jan 22, 2013 at 19:15

2 Answers 2

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.

4
  • Or maybe just $message = str_replace('SAD', 'HAPPY', $message);? Not super clear why that dollar sign is in there...
    – Nate Cook
    Jan 22, 2013 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! Jan 22, 2013 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, 2013 at 19:19
  • 1
    Thank you S&B! I mean @cryptic. S&B = Smart & Beautiful. :) Much appreciated! Jan 22, 2013 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.

4
  • You're answer worked as well @Jack. Thank you. What is the downfall of using a regular expression as opposed to the string replacement? Jan 22, 2013 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, 2013 at 19:43
  • @jack I am inclined to use the second part of your statement as a claim for my company. renoi.de/en :D Jun 5, 2018 at 13:38
  • @Lightningsoul go for it :)
    – Ja͢ck
    Jun 5, 2018 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.