【问题标题】:php need all posible changed valuesphp 需要所有可能的更改值
【发布时间】:2013-11-22 15:28:23
【问题描述】:

我需要更改字符串中的每个字符,但此更改包含所有可能的结果。数组变量的键和值可以互相替换

例如所有字母的列表类似于下面

$changeList=array(
"ş"=>"s",
"ç"=>"c",
"i"=>"ı",
"ğ"=>"ğ",
"ü"=>"u",
"ö"=>"o",
"Ş"=>"S",
"Ç"=>"C",
"İ"=>"I",
"Ğ"=>"G",
"Ü"=>"U",
"Ö"=>"O"
);

$word="ağaç";

如果我计算正确,这可能是 2^2 = 4 个结果。 (对于 ğ 和 ç 字母) 替换所有字母后,我需要这个结果。

  1. ağaç
  2. 阿加奇
  3. ağac
  4. 阿加克

和其他单词示例 =pişir 这个词包括可变的三个字母。 2^3 = 8 个结果。 (对于 s,ı,ç 3 个字母)

  1. pişir
  2. pışir
  3. pisir
  4. pişır
  5. pişır
  6. pisır
  7. 嘘
  8. pışır

我需要一个包含所有结果的数组变量。我怎样才能做到这一点,性能最好。

【问题讨论】:

  • 您的问题是什么?我不知道你在问什么
  • 当我给出两个参数时,我需要一个函数,word 和 changelist (array) 函数替换 word 中的更改列表字母,并在每次替换后添加 word 一个数组变量并返回一个包含所有结果的数组变量.对不起,我的英语不好
  • 我能想到一个几乎回答你的问题的实现,但我注意到pişir 说明了一个特定要求:你想为每个单独的字母都会发生变化 - 包括个多次出现的字母。这使事情变得有些复杂,因为您必须分析目标单词中的每个字母,对于初学者来说......我怀疑效率不是很高。如果一个单词中一个字母的多个实例可以一次性转换,那会简单得多,例如 strtr 之类的函数,但事实并非如此。
  • 多字节字符的存在也很复杂......

标签: php arrays replace


【解决方案1】:

好的,我对此有点着迷并决定尝试一下(嘿,现在是星期五晚上)。

正如我在上面的评论中所指出的,您希望为每个单独的字母变化生成一个新的单词变体 - 包括多次出现的字母。另一个棘手的考虑是多字节字符。

接下来是一个尝试,一个可能不太理想的答案,但希望能完成工作......我相信有很多方法可以改进这一点,但这是一个你可以迭代的开始:

<?php

// this solution assumes UTF-8 config settings in php.ini
// setting them with ini_set() seems to work also but ymmv
ini_set('default_charset', 'UTF-8');
ini_set('mbstring.internal_encoding', 'UTF-8');

function getWordReplacements($word, $replacements) {

    // $results... add the original
    $results = array($word);

    // multibyte split of $word
    // credit: http://stackoverflow.com/a/2556753/312962
    $chars = preg_split('//u', $word, -1, PREG_SPLIT_NO_EMPTY); 

    // we will iterate over the chars and create replacements twice,
    // first pass is a forward pass, second is a reverse pass
    // we need a copy of the $chars array for each pass
    $forwardPass = $chars;
    $reversePass = $chars;

    // how many chars in the word?
    $length = count($chars);
    // we'll store the index of the first character 
    // that has a copy in the $replacements list for later
    $first  = null;

    // first pass... iterate!
    for ($i = 0; $i < $length; $i++) {
        // is the current character in the $replacements list?
        if (array_key_exists($chars[$i], $replacements)) {
            // save the index of the first match
            if (is_null($first)) {
                $first = $i;
            }
            // replace the found char with the translation
            $forwardPass[$i] = $replacements[$forwardPass[$i]];
            // flatten the result and save it as a variation
            $results[] = implode($forwardPass);
        }
    }

    // second pass... same as the first pass except:
    // * we go backwards through the array
    // * we stop before we reach the $first index, to avoid a duplicate
    //   entry of the first variation saved in the first pass...
    for ($j = ($length - 1); $j > $first; $j--) {
        if (array_key_exists($chars[$j], $replacements)) {
            $reversePass[$j] = $replacements[$reversePass[$j]];
            $results[] = implode($reversePass);
        }
    }

    // return the results
    return $results;
}

// test with a subset replacement list
$list = [
    'ç' => 'c',
    'ğ' => 'g',
    'ş' => 's',
    'i' => 'ı',
];

// a couple of examples
$r1 = getWordReplacements('pişir', $list);
$r2 = getWordReplacements('ağaç', $list);

var_dump($r1, $r2);

产量:

array (size=6)
  0 => string 'pişir' (length=6)
  1 => string 'pışir' (length=7)
  2 => string 'pısir' (length=6)
  3 => string 'pısır' (length=7)
  4 => string 'pişır' (length=7)
  5 => string 'pisır' (length=6)
array (size=4)
  0 => string 'ağaç' (length=6)
  1 => string 'agaç' (length=5)
  2 => string 'agac' (length=4)
  3 => string 'ağac' (length=5)

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 2017-11-16
    • 2016-04-07
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多