Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?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 );

?>

>Solution :

However this would work, because this also includes the single quotes in the checking and replacing, so the yellow does not get processed twice

$searchColor = array ("'red'", "'brown'", "'yellow'");
$replacements = array ( "'purple'", "'chocolate'", "'yellow brown'");
echo str_replace( $searchColor, $replacements, $color);

RESULT

'purple', 'yellow brown', 'chocolate', 'pizza', 'moustard', 'mango', 'lemon'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading