php中字符串的索引,查找給定集合中特定字符串索引(PHP)的所有可能的排列組合...

這是我工作的解決方案:

function permute($str, $arr, $i, $n, &$result) {

$nLen = strlen($n);

// cycle through every position of needle

while (($i = strpos($str, $n, $i)) !== false) {

$i = $i + $nLen;

// cycle through each replacement value

foreach ($arr as $value) {

$modified = substr_replace($str, $value, $i - $nLen, $nLen);

// if there are no needles left, save it

if (stristr($modified, $n) === false) {

$result[] = $modified;

}

permute($modified, $arr, $i, $n, $result);

}

}

}

# each ? in the pattern to be replaced by all possible permutations of characters in chars array

# a through e as well as @ and .

$chars = range('a', 'e');

array_push($chars, '@');

array_push($chars, '.');

# the pattern to be checked

$pattern = "[email protected]?b?.ca";

$result = array();

$needle = '?';

$index = 0;

permute($pattern, $chars, $index, $needle, $result);

var_dump($result);

這是假設你只想要保存其中沒有針保持值。例如,而不是:

array(63) {

[0]=>

string(9) "[email protected]?.ca"

[1]=>

string(9) "[email protected]"

[2]=>

string(9) "[email protected]"

// etc...

這將輸出:

array(49) {

[0]=>

string(9) "[email protected]"

[1]=>

string(9) "[email protected]"

[2]=>

string(9) "[email protected]"

// etc...

如果你實際上想要做的第一個結果,然後簡單地去掉stristr($modified, $n) === false條件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值