英文:
phpsearch and replace but not duplicate
问题
我想使用PHP将单词“Dozenten”替换为“Dozentin”,将单词“Dozent”替换为“Dozenten”。现在的问题是,“Dozent”在两个单词中都出现了。我无法使用str_replace进一步处理。如何防止出现类似“Dozentenin”的情况?
英文:
I would like to use php to replace the word "Dozenten" with "Dozentin" and the word "Dozent" with "Dozenten". The problem now is that "Dozent" occurs in both words. I can't get any further with a str_replace. How can I prevent that I then get something like "Dozentenin"?
答案1
得分: 2
你可以使用strtr函数,这将翻译(替换)字符串中的子字符串:
<?php
$s = "Dozenten Dozent Dozent Dozenten";
echo strtr($s, array("Dozenten" => "Dozentin", "Dozent" => "Dozenten"));
英文:
You can make use of the strtr function, this will translate (replace) substrings in your string:
<?php
$s = "Dozenten Dozent Dozent Dozenten";
echo strtr($s, array("Dozenten" => "Dozentin", "Dozent" => "Dozenten"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论