php搜索和替换但不重复

huangapple go评论61阅读模式
英文:

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:

&lt;?php

$s = &quot;Dozenten Dozent Dozent Dozenten&quot;;

echo strtr($s, array(&quot;Dozenten&quot; =&gt; &quot;Dozentin&quot;, &quot;Dozent&quot; =&gt; &quot;Dozenten&quot;));

Try it online

huangapple
  • 本文由 发表于 2023年7月17日 14:41:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定