PHP替换符号所有可能的变体

Wiz*_*ard 2 php combinations replace

我有我想要替换的数组符号,但我需要生成所有可能性

$lt = array(
    'a' => '?',
    'e' => '?',
    'i' => '?',
);
Run Code Online (Sandbox Code Playgroud)

例如,如果我有这个字符串:

tazeki
Run Code Online (Sandbox Code Playgroud)

可能会有大量的结果:

t?zeki
taz?ki
t?z?ki
tazek?
t?zek?
taz?k?
t?z?k?
Run Code Online (Sandbox Code Playgroud)

我的问题是什么配方用于拥有所有变种?

Riz*_*123 5

这应该对你有用,简单易行:

这段代码有什么作用?

1.数据部分

在数据部分中,我只使用关联数组(搜索字符作为键,替换为值)定义字符串和单个字符的替换.

2. getReplacements()功能

此函数获取必须以此格式替换的字符的所有组合:

key   = index in the string
value = character
Run Code Online (Sandbox Code Playgroud)

所以在这个代码示例中,数组看起来像这样:

Array (
    [0] => Array (
        [1] => a
    )    
    [1] => Array (
        [3] => e
    )   
    [2] => Array (
        [3] => e
        [1] => a
    )    
    [3] => Array (
        [5] => i
    )    
    [4] => Array (
        [5] => i
        [1] => a
    )    
    [5] => Array (
        [5] => i
        [3] => e
    )    
    [6] => Array (
        [5] => i
        [3] => e
        [1] => a
    )

)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,此数组包含必须替换的字符的所有组合,格式如下:

[0] => Array (
     //^^^^^ The entire sub array is the combination which holds the single characters which will be replaced
    [1] => a
   //^     ^ A single character of the full combination which will be replaced
   //| The index of the character in the string (This is that it also works if you have a character multiple times in your string)
   // e.g. 1 ->  t *a* z e k i
   //            ^  ^  ^ ^ ^ ^
   //            |  |  | | | |
   //            0 *1* 2 3 4 5
)    
Run Code Online (Sandbox Code Playgroud)

那么它如何获得所有组合?

非常简单我循环遍历每个我想用foreach循环替换的单个字符,然后我遍历我已经拥有的每一个组合,并将它与当前为foreach循环的值的字符组合.

但要使其工作,您必须从一个空数组开始.所以作为一个简单的例子来看和理解我的意思:

Characters which have to be replaced (Empty array is '[]'): [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

                               //new combinations for the next iteration
                               |
Character loop for NAN*:

    Combinations:
                  - []         |  -> []

Character loop for 1:

    Combinations:
                  - []    + 1  |  -> [1]    

Character loop for 2:

    Combinations:
                  - []    + 2  |  -> [2]
                  - [1]   + 2  |  -> [1,2]    

Character loop for 3:

    Combinations:
                  - []    + 3  |  -> [3]
                  - [1]   + 3  |  -> [1,3]
                  - [2]   + 3  |  -> [2,3]         
                  - [1,2] + 3  |  -> [1,2,3]    
                               //^ All combinations here
Run Code Online (Sandbox Code Playgroud)

*NAN:不是数字

所以你可以看到总有:(2^n)-1组合.同样从这个方法中,组合数组中还有一个空数组,所以在返回数组之前,我只是array_filter()用来删除所有空数组并array_values()重新索引整个数组.

3.更换部件

因此,为了从字符串中获取所有将构建组合的字符,我使用此行:

array_intersect(str_split($str), array_keys($replace))
Run Code Online (Sandbox Code Playgroud)

这只是array_intersect()从字符串作为数组str_split()和来自替换数组的键的所有巧合array_keys().

在这段代码中,传递给getReplacements()函数的数组看起来像这样:

Array
(
    [1] => a
   //^     ^ The single character which is in the string and also in the replace array
   //| Index in the string from the character
    [3] => e
    [5] => i
)
Run Code Online (Sandbox Code Playgroud)

4.替换所有组合

最后,您只需要用replace数组替换源字符串中的所有组合.为此,我循环遍历每个组合,并从替换数组中的匹配字符组合替换字符串中的每个字符.

这可以通过以下方式完成:

$tmp = substr_replace($tmp, $replace[$v], $k, 1);
     //^^^^^^^^^^^^^^       ^^^^^^^^^^^^  ^^  ^ Length of the replacement
     //|                    |             | Index from the string, where it should replace
     //|                    | Get the replaced character to replace it
     //| Replaces every single character one by one in the string 
Run Code Online (Sandbox Code Playgroud)

有关substr_replace()手册的更多信息,请访问:http://php.net/manual/en/function.substr-replace.php

在此行之后,您只需在结果数组中添加替换的字符串,然后将字符串重新放入源字符串.


码:

<?php

    //data
    $str = "tazeki"; 

    $replace = array(
        'a' => '?',
        'e' => '?',
        'i' => '?',
    );


    function getReplacements($array) {

        //initalize array
        $results = [[]];

        //get all combinations
        foreach ($array as $k => $element) {
            foreach ($results as $combination)
                $results[] = [$k => $element] + $combination;
        }

        //return filtered array
        return array_values(array_filter($results));

    }

    //get all combinations to replace
    $combinations = getReplacements(array_intersect(str_split($str), array_keys($replace)));

    //replace all combinations
    foreach($combinations as $word) {
        $tmp = $str;
        foreach($word as $k => $v)
            $tmp = substr_replace($tmp, $replace[$v], $k, 1);
        $result[] = $tmp;
    }

    //print data
    print_r($result);

?>
Run Code Online (Sandbox Code Playgroud)

输出:

Array
(
    [0] => t?zeki
    [1] => taz?ki
    [2] => t?z?ki
    [3] => tazek?
    [4] => t?zek?
    [5] => taz?k?
    [6] => t?z?k?
)
Run Code Online (Sandbox Code Playgroud)