Florianb
Florianb

Reputation: 188

Replacing string with file_get_contents()

I'm trying to replace a string in a variable which got the content via file_get_contents().

$link_yes = "someText";
$link_no = "someText";
ob_start();
$mail_content = file_get_contents($_SERVER['DOCUMENT_ROOT']. $user_file . $mail_file);
$link1 = array("###Text to replace###");
$link2 = array("###Text to replace###");
str_replace($link1, $link_yes, $mail_content);
str_replace($link2, $link_no, $mail_content);
$mail -> Body = $mail_content;
ob_end_clean();

I tried it without $link1 and $link2. So I pasted the string directly in the replace-function.

But it didn't work either.

Can somebody help me?

Thanks!

Upvotes: 2

Views: 1562

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

str_replace() returns the amended string and you are not capturing that returned amended value.

Also unless you have more than one thing to replace you dont have to use an array, although that would not be an error

$link_yes = "someText";
$link_no = "someText";

ob_start();
$mail_content = file_get_contents($_SERVER['DOCUMENT_ROOT']. $user_file . $mail_file);

$link1 = "###Text to replace###";
$link2 = "###Text to replace###";

$mail_content = str_replace($link1, $link_yes, $mail_content);
$mail_content = str_replace($link2, $link_no, $mail_content);


$mail -> Body = $mail_content;

ob_end_clean();

Upvotes: 1

Related Questions