Viewed   219 times

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.

 Answers

4
$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.

Wednesday, December 14, 2022
 
yotaxp
 
1

(?<!,)s

That uses a negative lookbehind to match all spaces (s) that aren't followed by a ,.

preg_replace("/(?<!,)s/", '-', $mystring);

Play with the regex here.

Thursday, October 20, 2022
 
marcg
 
2

So I managed to create my own function in the end. Thanks for help and inspiration though.

function replaceWithCase($source, $replacement, $string, $pos = 0) {

    while (($pos = strpos(strtolower($string), strtolower($source), $pos))!== false) {
        $substr = mb_substr($string, $pos, strlen($source));
        $remaining = mb_substr($string, $pos + strlen($source));

        if (ctype_upper($substr)) {
            $string = substr_replace($string,strtoupper($replacement),$pos,strlen($source));
            continue;
        }

        $substrParts = preg_split('//u', $substr, null, PREG_SPLIT_NO_EMPTY);
        $replaceParts = preg_split('//u', $replacement, null, PREG_SPLIT_NO_EMPTY);
        $newWord = '';

        foreach ($replaceParts as $k => $rp) {
            if (array_key_exists($k,$substrParts))
                $newWord .= ctype_upper($substrParts[$k]) ? mb_strtoupper($rp) : mb_strtolower($rp);
            else
                $newWord .= $rp;  
        }
        $string = substr_replace($string,$newWord,$pos,strlen($source));
        $pos = $pos + strlen($source);
    }

    return $string;
}

echo replaceWithCase("place", "garden", "Potato is jumping all over the PLACE");
echo "<br>";
echo replaceWithCase("jumping", "running", "Potato is jumping all over the pLAcE");
echo "<br>";
echo replaceWithCase("jumping", "cry", "Potato is jumping all over the place");
echo "<br>";
echo replaceWithCase("all", "", "Potato is jumping all over the Place");
echo "<br>";
echo replaceWithCase(" ", ";", "Potato is jumping all over the Place", 10);
echo "<br>";

Output:

Potato is jumping all over the GARDEN
Potato is running all over the pLAcE
Potato is cry all over the place
Potato is jumping over the Place
Potato is jumping;all;over;the;Place
Friday, September 9, 2022
3

You could try using simple_xml, or better DOMDocument (http://www.php.net/manual/en/class.domdocument.php) before you make it a valid html code, and use this functionality to find the nodes you are looking for, and replace them, for this you could try XPath (http://w3schools.com/xpath/xpath_syntax.asp).

Edit 1:

Take a look at the answer of this question:

RegEx match open tags except XHTML self-contained tags

Saturday, August 20, 2022
 
2

You can try htmlentities() to convert all entities including the euro sign, so they appear like &euro;.

I would use it in the following manner: htmlentities($str, ENT_QUOTES|"ENT_HTML401", "UTF-8", true)

You may choose to use: htmlentities($XML_COMMENT, ENT_QUOTES | ENT_IGNORE, "UTF-8", true). For a full explanation of what the flags change, visit the link below. As requested by OP @baptme (see comments).

Source: php.net reference

Thursday, October 20, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :