How can one ensure that the text in its original case is not affected while using str_replace() for replacements in PHP?

When using str_replace() in PHP for replacements, the original case of the text can be preserved by first storing the original text in a variable before performing the replacements. Then, when replacing the text, make sure to use the original text variable for comparison to maintain the original case.

// Original text
$text = "This is a Sample Text";

// Store original text in a variable
$original_text = $text;

// Perform replacements
$text = str_replace("sample", "replacement", $text);

// Use original text variable for comparison
if(strtolower($text) == strtolower($original_text)){
    $text = $original_text;
}

echo $text; // Output: This is a Sample Text