php中有多个字符串替换

php

$nn="ab bc cd cd ab";
$tttt=str_ireplace("cd","aaa",$nn);
$tt=str_ireplace("ab","aaa",$tttt);

but the below coding is not working

$nn="ab bc cd cd ab";
$tttt=str_ireplace("cd" or "ab","aaa",$nn);

The output is "aaa bc aaa aaa aaa".PLease help me in simplyfying it.because there are lot more str replaces for various words.

You may use preg_replace which accepts regex as the first argument. ab|cd would match ab or cd.

preg_replace('~cd|ab~i', 'aaa', $nn);

And add i modifier for doing case-insensitive match.

You use array in first parameter of str_ireplace.

$nn="ab bc cd cd ab";
$replace_words = array("ab", "cd");
$tttt = str_ireplace($replace_words, "aaa", $nn);