search

Home  >  Q&A  >  body text

php search and replace but not duplicate.

I want to replace the word "Dozenten" with "Dozentin" and the word "Dozent" with "Dozenten" using PHP. The problem is, "Dozent" appears in both words. I can't continue using `str_replace`. How to avoid getting results similar to "Dozentenin"?

P粉481815897P粉481815897486 days ago492

reply all(1)I'll reply

  • P粉265724930

    P粉2657249302023-07-18 14:14:29

    你可以使用`strtr`函数来实现,它可以在字符串中进行子字符串的翻译(替换):

    <?php 
    $s = "Dozenten Dozent Dozent Dozenten"; 
    echo strtr($s, array("Dozenten" > "Dozentin", "Dozent" => "Dozenten")); 

    reply
    0
  • Cancelreply