所以...我取出了区分大小写的部分,然后我意识到这可能是你的规则的一部分,所以你可能需要重新添加它。我需要去上班,所以我没有时间解释所有这些。我需要使用数组指针,因此您可能需要阅读next()和current()。
编辑:我添加了很多评论来尝试帮助您了解这里发生的事情。
<?php
// this is an array of the input values to use below
$strings_to_test = array('aifkcwh', 'aifkcwhsz', 'kim', 'aif');
// get all possible combianations of $kantha+$talu+$murdha
$combinations = get_string_combinations();
display($combinations,'$combinations');
foreach ($strings_to_test as $stti => $string) // 'aifkcwh', 'aifkcwhsz', 'kim', 'aif'
{
$values = array($string);
reset($values);
display("<br/>-----------------------------------------");
display("value: $string"); // first iteration "aifkcwh"
// loop through $values using an array pointer
// while the current array pointer position is not null/false
while(current($values)!==false)
{
// on the first iteration, $values will be the inputted string
// from $strings_to_test... our example is "aifkcwh"
$value = current($values);
display("current value: $string");
// for each possible combination of $kantha.$talu.$murdha
// let's say our first combination is "aif"
foreach($combinations as $ci => $combination)
{
// look and see if the current value we are looking
// at ("aifkcwh") contains this combination string
if (stripos($value,$combination)!==false)
{
// if it does... we perform the string mutation
// "aif" does exist in the string "aifkcwh"
// get the second letter in the combination string
$single = $combination[1]; // i
// double that second letter
$double = str_repeat($single, 2); // ii
// create a new string that is the combination with the
// second letter doubled...
$newcom = str_ireplace($single, $double, $combination); // aiif
// replace that string in $values so that
// aifkcwh becomes aiifkcwh
$newval = str_ireplace($combination, $newcom, $value); // aiifkcwh
// does the new value ("aiifkcwh") exist in $values?
// have we already recorded this mutation?
if (!in_array($newval,$values))
{
// if not... append it to the array $values
$values[] = $newval;
display("added to array for $value: $newval");
// now, values would go from being = array([0]=>'aifkcwh');
// to array ([ 0] => 'aifkcwh', [1] => 'aiifkcwh' );
}
else
{
display("not added because it already exists: $newval");
}
}
} // <-- end of the foreach statement, this will go through all combinations
// in our combinations array for this particular value which is currently aifkcwh
// next($values) increments the array pointer so that we move to the next
// value in the $values array. since we just added a value,
// $values now contains array ([ 0] => 'aifkcwh', [1] => 'aiifkcwh' );
// before this statement index 0, current($values) == 'aifkcwh'
next($values);
// after this statement index 1, current($values) == 'aiifkcwh'
// for the next loop, we will test this string for all the combinations
// if there is no next value, the `while` loop will end
}
// after we have gone through every possible combination for "aifkcwh",
// we will have something like this:
/*
Array
(
[0] => aifkcwh
[1] => aiifkcwh
[2] => aifkccwh
[3] => aiifkccwh
)
*/
// and we add that to the $output array that contains an index for
// each input string, which contains all possible mutations of that string
$output[$string] = $values;
}
// display the results.
display($output,'$output');
// ================================================================
// functions
// ================================================================
// get all possible combinations of $kantha.$talu.$murdha
function get_string_combinations()
{
$kantha = array("a","k","g","n","h");
$talu = array("i","c","j","y","s");
$murdha = array("f","w","q","r","z");
$combinations = array();
foreach($kantha as $k) // "a","k","g","n","h"
{
foreach($talu as $t) // "i","c","j","y","s"
{
foreach($murdha as $m) // "f","w","q","r","z"
{
$combinations[] = $k.$t.$m;
}
}
}
// this gives us an array if 125 items
/*
$combinations =
Array
(
[0] => aif
[1] => aiw
[2] => aiq
[3] => air
[4] => aiz
....
[121] => hsw
[122] => hsq
[123] => hsr
[124] => hsz
)
*/
return $combinations;
}
// display variables to see what the code is doing
function display($var,$label=null)
{
if (is_array($var))
{
print "<pre>$label = \n".print_r($var,true)."</pre>";
}
else print "$var <br/>";
}
?>
程序的完整输出除了$combinations
因为它相当长......
-----------------------------------------
value: aifkcwh
current value: aifkcwh
added to array for aifkcwh: aiifkcwh
added to array for aifkcwh: aifkccwh
current value: aifkcwh
added to array for aiifkcwh: aiifkccwh
current value: aifkcwh
not added because it already exists: aiifkccwh
current value: aifkcwh
-----------------------------------------
value: aifkcwhsz
current value: aifkcwhsz
added to array for aifkcwhsz: aiifkcwhsz
added to array for aifkcwhsz: aifkccwhsz
added to array for aifkcwhsz: aifkcwhssz
current value: aifkcwhsz
added to array for aiifkcwhsz: aiifkccwhsz
added to array for aiifkcwhsz: aiifkcwhssz
current value: aifkcwhsz
not added because it already exists: aiifkccwhsz
added to array for aifkccwhsz: aifkccwhssz
current value: aifkcwhsz
not added because it already exists: aiifkcwhssz
not added because it already exists: aifkccwhssz
current value: aifkcwhsz
added to array for aiifkccwhsz: aiifkccwhssz
current value: aifkcwhsz
not added because it already exists: aiifkccwhssz
current value: aifkcwhsz
not added because it already exists: aiifkccwhssz
current value: aifkcwhsz
-----------------------------------------
value: kim
current value: kim
-----------------------------------------
value: aif
current value: aif
added to array for aif: aiif
current value: aif
$output =
Array
(
[aifkcwh] => Array
(
[0] => aifkcwh
[1] => aiifkcwh
[2] => aifkccwh
[3] => aiifkccwh
)
[aifkcwhsz] => Array
(
[0] => aifkcwhsz
[1] => aiifkcwhsz
[2] => aifkccwhsz
[3] => aifkcwhssz
[4] => aiifkccwhsz
[5] => aiifkcwhssz
[6] => aifkccwhssz
[7] => aiifkccwhssz
)
[kim] => Array
(
[0] => kim
)
[aif] => Array
(
[0] => aif
[1] => aiif
)
)