'How to change a text in PHP?

I'm trying to change the name of three attributes using a string in PHP, but I'm stuck. Could you help me?

This is the code that I had:

function color () {
    switch ($color) {
    case "red":
        echo "purple";
        break;
    case "yellow":
        echo "yellow brown";
        break;
    case "brown":
        echo "chocolate";
        break;
    }
}

Also, I was trying using the same with a string but I'm not sure if it's okey:

<?php

$color = "'red', 'yellow', 'brown', 'pizza', 'moustard', 'mango', 'lemon'"; 

$searchColor = array ('red','yellow', 'brown');

$replacements = array ( 'purple', 'yellow brown','chocolate');

echo str_replace( $searchColor, $replacements, $color );

?>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source