如何使用 str_replace 或其他函数仅更改 word 中的字符串一次?
How to change string inside word only once using str_replace or other function?
我需要更改字符串中的一些单词 - 我必须在 2 个或更多单词上分隔字符串 - 或者在单词前后添加 space。例如我有 shopifystore - 这必须分成 2 个词“:shopify 和商店,所以结果必须是:"shopify store"。再举一个例子 - 我有 dogsstore - 这必须分成 2 个词“:dogs 和 store , 所以结果必须是: "dogs store"
所以,我写了一些功能,但是效果不是很好。我的函数:
function englishchange($string) {
$latin = array('dogs','dog','stores','store','shops','shop','shopify');
$latinchanged = array(' dogs ',' dog ',' stores ',' store ',' shops ',' shop ',' shopify ');
return str_replace($latin, $latinchanged, $string);
}
$englishchanged = (englishchange('shopifystore'));
但是 "dogsstore" 的结果是:"dog s store" 并且 "shopifystore" 将变为:"shop ify store"。谁能帮我重写 php 代码以获得正确的结果?
您可以使用 strtr
in its second form to do the replacements. In that mode, it takes an array of replacement pairs and, working from the longest strings downward, having made a replacement, it will not replace that substring again. So you just need to combine your $latin
and $latinchanged
arrays into an array using array_combine
然后调用 strtr
:
function englishchange($string) {
$latin = array('dogs','dog','stores','store','shops','shop','shopify');
$latinchanged = array(' dogs ',' dog ',' stores ',' store ',' shops ',' shop ',' shopify ');
return strtr($string, array_combine($latin, $latinchanged));
}
$englishchanged = (englishchange('dogsstore shopifystore'));
echo $englishchanged;
输出:
dogs store shopify store
问题是该字符串与多个搜索项匹配。
您可以假设每个字符串只需要一个更改,因此您可以继续循环以避免出现问题:
function englishchange($string)
{
$latin = array('dogs','dog','stores','store','shops','shop','shopify');
$latinchanged = array(' dogs ',' dog ',' stores ',' store ',' shops ',' shop ',' shopify ');
foreach ($latin as $key => $item) {
if (strpos($string, $item) !== false) {
return str_replace($item, $latinchanged[$key], $string);
}
}
}
这样修改后的字符串将在第一次替换后返回。
我需要更改字符串中的一些单词 - 我必须在 2 个或更多单词上分隔字符串 - 或者在单词前后添加 space。例如我有 shopifystore - 这必须分成 2 个词“:shopify 和商店,所以结果必须是:"shopify store"。再举一个例子 - 我有 dogsstore - 这必须分成 2 个词“:dogs 和 store , 所以结果必须是: "dogs store"
所以,我写了一些功能,但是效果不是很好。我的函数:
function englishchange($string) {
$latin = array('dogs','dog','stores','store','shops','shop','shopify');
$latinchanged = array(' dogs ',' dog ',' stores ',' store ',' shops ',' shop ',' shopify ');
return str_replace($latin, $latinchanged, $string);
}
$englishchanged = (englishchange('shopifystore'));
但是 "dogsstore" 的结果是:"dog s store" 并且 "shopifystore" 将变为:"shop ify store"。谁能帮我重写 php 代码以获得正确的结果?
您可以使用 strtr
in its second form to do the replacements. In that mode, it takes an array of replacement pairs and, working from the longest strings downward, having made a replacement, it will not replace that substring again. So you just need to combine your $latin
and $latinchanged
arrays into an array using array_combine
然后调用 strtr
:
function englishchange($string) {
$latin = array('dogs','dog','stores','store','shops','shop','shopify');
$latinchanged = array(' dogs ',' dog ',' stores ',' store ',' shops ',' shop ',' shopify ');
return strtr($string, array_combine($latin, $latinchanged));
}
$englishchanged = (englishchange('dogsstore shopifystore'));
echo $englishchanged;
输出:
dogs store shopify store
问题是该字符串与多个搜索项匹配。 您可以假设每个字符串只需要一个更改,因此您可以继续循环以避免出现问题:
function englishchange($string)
{
$latin = array('dogs','dog','stores','store','shops','shop','shopify');
$latinchanged = array(' dogs ',' dog ',' stores ',' store ',' shops ',' shop ',' shopify ');
foreach ($latin as $key => $item) {
if (strpos($string, $item) !== false) {
return str_replace($item, $latinchanged[$key], $string);
}
}
}
这样修改后的字符串将在第一次替换后返回。