替换php文件箱上的文字

替换php文件箱上的文字,php,regex,Php,Regex,如何将一些“整词”替换为另一个保持相同大写(小写、首字母或全部)和源词单数/复数的词 如果我有这个: $text="This is a text about animals Word1 and Letters1. We also talk about animals words2, letter2 and WORDS3, LETTERS3. And text woRd4, LEtter4 and LETTEr5 and wordpress"; 及 我希望得到: $text="This is

如何将一些“整词”替换为另一个保持相同大写(小写、首字母或全部)和源词单数/复数的词

如果我有这个:

$text="This is a text about animals Word1 and Letters1. 
We also talk about animals words2, letter2 and WORDS3, LETTERS3. 
And text woRd4, LEtter4 and LETTEr5 and wordpress";

我希望得到:

$text="This is a text about animals Cat1 and Dogs1. 
We also talk about animals cats2, dog2 and CATS3, DOGS3. 
And text woRd4, LEtter4 and LETTEr5 and wordpress";
总之:

$text="This is a text about animals Word1 and Letters1. 
    We also talk about animals words2, letter2 and WORDS3, LETTERS3. 
    And text woRd4, LEtter4 and LETTEr5";

function replace($replacements, $text)
{
    $_replacements=[];

    // Add the three possible patterns
    foreach($replacements as $search=>$replace)
    {
        $_replacements[strtolower($search)]=strtolower($replace);
        $_replacements[strtoupper($search)]=strtoupper($replace);
        $_replacements[ucfirst(strtolower($search))]=ucfirst(strtolower($replace));
    }

    return str_replace(array_keys($_replacements), array_values($_replacements), $text);
}

echo(replace($replacements, $text));
我有一个词和一个替换词:例如,“字母”和“狗”,我想:

  • 用小写字母替换与该单词匹配的所有单词,用“dog”替换“letter”
  • 用大写字母替换与该单词匹配的所有单词:“字母”改为“狗”
  • 用大写字母中的第一个字母替换与该单词匹配的所有单词:“字母”替换为“狗”
  • 我只想在阵列中使用一种组合,而不是三种可能的组合,因为阵列是从配置文件读取的:

    我想在配置中写入以下内容:

    $replacements=['word'=>'cat','letter'=>'dog']

    而不是这个:

    $replacements=['word'=>'cat'、'word'=>'cat'、'word'=>'cat'、'letter'=>'dog',
    “字母”=>“狗”,“字母”=>“狗”]

    我找到的最佳解决方案是:

    $text="This is a text about animals Word1 and Letters1. 
        We also talk about animals words2, letter2 and WORDS3, LETTERS3. 
        And text woRd4, LEtter4 and LETTEr5";
    
    function replace($replacements, $text)
    {
        $_replacements=[];
    
        // Add the three possible patterns
        foreach($replacements as $search=>$replace)
        {
            $_replacements[strtolower($search)]=strtolower($replace);
            $_replacements[strtoupper($search)]=strtoupper($replace);
            $_replacements[ucfirst(strtolower($search))]=ucfirst(strtolower($replace));
        }
    
        return str_replace(array_keys($_replacements), array_values($_replacements), $text);
    }
    
    echo(replace($replacements, $text));
    
    但是,此解决方案将WordPress替换为CatPress:(

    是否有更好和/或更有效的解决方案?是否有使用regex的解决方案?

    PS:您可以在此处测试我的代码:

    PS:这一行:
    和文本字4、字母4和字母5
    表示所有其他可能的大小写组合都不重要


    PS:我在stackoverflow中发现的类似问题是关于格式化和突出显示的,它的解决方案不适用于这种情况(或者我不知道如何应用它)

    一个处理不规则复数的版本:

    $text = 'This is a text about animals, Children, Word1 and Letters1. 
    We also talk about animals words2, letter2 and WORDS3, LETTERS3. 
    And text woRd4, LEtter4 and LETTEr5';
    
    // replacements
    $regular = [ 'word' => 'cat', 'letter' => 'dog', 'animal' => ['child', 'children'] ];
    $nonregular = [ 'child' => 'animal', 'children' => 'animals' ];
    
    $result = preg_replace_callback('~[a-z]+(?:(?<=(s)))?~i', function ($m) use ($regular, $nonregular) {
        $lower = strtolower($m[0]);
        $rep = $m[0];
        if ( isset($nonregular[$lower]) ) {
            $rep = $nonregular[$lower];
        } elseif ( isset($regular[$lower]) ) {
            $rep = is_array($regular[$lower]) ? $regular[$lower][0] : $regular[$lower];
        } elseif ( isset($m[1]) ) {
            $sing = substr($lower, 0, -1);
            if ( isset($regular[$sing]) )
                $rep = is_array($regular[$sing]) ? $regular[$sing][1] : $regular[$sing] . 's';
        } else {
            return $rep;
        }
    
        if ( $m[0] == $lower )
            return $rep;
        elseif ($m[0] == strtoupper($lower) )
            return strtoupper($rep);
        elseif ( $m[0] == ucfirst($lower) )
            return ucfirst($rep);
    
        return $rep;
    }, $text);
    
    echo $text, PHP_EOL, PHP_EOL, $result;
    
    $text=”这是一个关于动物、儿童、单词1和字母1的文本。
    我们还讨论了动物的单词2,字母2和单词3,字母3。
    以及文字4、字母4和字母5’;
    //替代品
    $regular=['word'=>'cat','letter'=>'dog','animal'=>['child','children']];
    $nonregular=['child'=>'animal','children'=>'animals'];
    
    $result=preg_replace_回调('~[a-z]+(?):(?您的第3行令人困惑,除第3行外,第1行和第2行都被替换了?您到底想达到什么目的?您的目标是替换“忽略上下”一词?但为什么您的字母4没有被替换。如果替换的字母多或少,我们如何保持这种情况?我理解处理大写字母和l的所有可能组合小写字母是不可能的。尤其是如果您在搜索和替换之间更改字母数。这就是为什么我只选择了三种模式或组合以及第三行(
    和text woRd4、LEtter4和LETTEr5
    )表示所有其他组合都不重要我希望您在一小时后添加的所有额外信息,特别是在处理“现在”(和永远)搜索之前,不要有更大的查找数组的要求已删除的工作解决方案。但是,真的;如果查找时间长一点又有什么关系呢?如果有效-它有效。服务器不会因几个额外的数组元素而过载和融化。你听说过吗?谢谢。我真的很喜欢管理不规则复数的可能性。@Juanantonitubío:谢谢,但请注意对IREGUL的支持ar复数实际上是非常基本的,因为您必须在非规则数组中包含每种形式的单词。最好编写一种结构,使具有相同复数结尾的单词在一起(最可能),例如:
    ['s'=>['wd1'=>'rp1','wd2'=>'rp2'…],'es'=>['wd3'=>'rp3'…],…]
    并最终构建一个索引以快速检索它们,或者使用字典自动构建包含所有表单的哈希。