我一直在這裏擱置了我的大腦幾天,我似乎被卡住了。我是PHP新手,請原諒任何不合適或錯誤。查找給定集合中特定字符串索引(PHP)的所有可能的排列組合
給定一個模式,例如類似電子郵件地址「[email protected]?b?.ca」我需要替換'?'的任何實例。與來自一組字符'a-e','@'和''的所有可能的排列組合在一起。
這就是我現在所擁有的:
function permute($str, $ch, $i, $n, &$output) {
if ($i == $n) {
array_push($output, $str);
} else {
for ($x = $i; $x < $n; $x++) {
for ($y = 0; $y < count($ch); $y++) {
$str[$x] = $ch[$y];
permute($str, $ch, $i + 1, $n, $output);
}
}
}
}
# each ? in the pattern to be replaced by all possible permutations of characters in chars array
# a through e as well as @ and .
$output = array();
$chars = range('a', 'e');
array_push($chars, '@');
array_push($chars, '.');
# the pattern to be checked
$pattern = "[email protected]?b?.ca";
permute($pattern, $chars, 0, strlen($pattern), $output);
...這是非常接近我想要的,但不完全正確。該函數對字符串的每個字符進行操作,但它只應在'?'上進行。還有什麼我可以做的,我失蹤了?如果我想出來,我會在評論中回覆並編輯!
預期結果是什麼? –
以上例子:[email protected],[email protected],[email protected] ... [email protected],[email protected],[email protected] ...等等。 –