Gopiga Jothiraj
Gopiga Jothiraj

Reputation: 11

multiple string replace in 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.

Upvotes: 0

Views: 106

Answers (2)

Samir Selia
Samir Selia

Reputation: 7065

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

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174844

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.

Upvotes: 0

Related Questions