search

Home  >  Q&A  >  body text

How to use str_replace in php to search and replace multiple words?

For example, string:

I|You|He

How to replace with

Pig|Cat|Dog

$str = "I|You|He";
$replace_str = str_replace(array("I","You","He"),array("Pig","Cat","Dog"),$str);
print($replace_str);

php output:
    "I|you|dog"
PHP中文网PHP中文网2864 days ago614

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-16 13:11:26

    How did the poster test it and under what environment? The code provided by the poster is the correct answer

    reply
    0
  • Cancelreply