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? Commented 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. Commented 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. Commented 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. Commented Jan 22, 2013 at 19:14
  • 1
    Also, you had better use explicit PHP tags - <?php, not <?. Just to pick up a good habit ;-) Commented 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Or maybe just $message = str_replace('SAD', 'HAPPY', $message);? Not super clear why that dollar sign is in there...
YOU GOT IT. That worked. I will mark the answer correct when it allows me to. You rock. Thank you so much!
@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.
Thank you S&B! I mean @cryptic. S&B = Smart & Beautiful. :) Much appreciated!
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 Comments

You're answer worked as well @Jack. Thank you. What is the downfall of using a regular expression as opposed to the string replacement?
@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 :)
@jack I am inclined to use the second part of your statement as a claim for my company. renoi.de/en :D
@Lightningsoul go for it :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.