我需要转换形式的字符串
"a b c"
进入表格的数组
Array
(
[0] => a
[1] => a b
[2] => a b c
[3] => b
[4] => b c
[5] => c
)
PHP 是否提供将字符串转换为所有子字符串的 native 函数?如果不是,获取所有子串的阻力最小的路径是什么?是否有一种直接的方法可以对字符串进行 explode(),并使用数组运算符生成所有 [有序] 排列?
干杯!
最佳答案
使用 in-php-array-is-the-duct-tape-of-the-universe 方式 :P
function get_all_substrings($input, $delim = '') {
$arr = explode($delim, $input);
$out = array();
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i; $j < count($arr); $j++) {
$out[] = implode($delim, array_slice($arr, $i, $j - $i + 1));
}
}
return $out;
}
$subs = get_all_substrings("a b c", " ");
print_r($subs);
关于php - 如何在 PHP 中查找字符串的所有子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2099732/