I am trying to change the color to red on certain words on my site. However the below code changes each word to "array".

Can someone tell me what is wrong with my code?

function replace_content($content)
    $newwords = array("Check", "Map", "Comments", "Send", "Print");
    $content = str_replace($newwords, '<span style="color:red">' . $newwords . '</span>', $content);
    return $content;
  • That happens, because you've put an array into the string: '<span style="color:red">' . $newwords . '</span>'. – fuxia Sep 19 '15 at 20:02
  • Thanks, this led me to further research and I was able to get it to work with this: – Brendan Sep 19 '15 at 20:37
function replace_content($text) {
  $replace = array(
    'Check' => '<span style="color:red">Check</span>',
    'Map' => '<span style="color:red">Map</span>',
    'Comments' => '<span style="color:red">Comments</span>',
    'Print' => '<span style="color:red">Print</span>'

  $text = str_replace(array_keys($replace), $replace, $text);
  return $text;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.