獲取中英文字符串按序全排列

/**

* 獲取字符串按序全排列(英文) e.g. abcd => a ab abc abcd b bc bcd c cd d

*

* @param $str

* @param string $separator

* @return array

*/

function getSortCombinations($str, $separator = '') {

? ? if ($separator !== '') {

? ? ? ? $chars = explode($separator, $str);

? ? } else {

? ? ? ? $chars = str_split($str);

? ? }

? ? $res = $chars;

? ? $count = count($chars);

? ? for ($i = 0; $i < $count - 1; $i++) {

? ? ? ? $tmp = $chars[$i];

? ? ? ? for ($j = $i + 1; $j < $count; $j++) {

? ? ? ? ? ? $tmp = $tmp . $chars[$j];

? ? ? ? ? ? $res[] = $tmp;

? ? ? ? }

}

? ? return $res;

}

/**

* 獲取字符串按序全排列(中文)

*

* @param $str

* @return array

*/

function getSortCombinationsChinese($str) {

? ? $chars = preg_split('/(?<!^)(?!$)/u', $str);

? ? $res = $chars;

? ? $count = count($chars);

? ? for ($i = 0; $i < $count - 1; $i++) {

? ? ? ? $tmp = $chars[$i];

? ? ? ? for ($j = $i + 1; $j < $count; $j++) {

? ? ? ? ? ? $tmp = $tmp . $chars[$j];

? ? ? ? ? ? $res[] = $tmp;

? ? ? ? }

}

? ? return $res;

}

推薦:?浮生無事的博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,216評論 0 3
  • 在C語言中,五種基本數(shù)據(jù)類型存儲空間長度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 4,066評論 0 2
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,153評論 0 2
  • 在北京的5天轉(zhuǎn)瞬即逝,在火車上,看著照片,不禁潸然淚下。 回憶今天,是本次研學(xué)的最后一天,我們?nèi)ケ本﹨⒂^了清華大學(xué)...
    張嘉燕閱讀 204評論 0 0
  • 想了很多煽情的話,開始寫的時候卻忘記了,但唯獨和你說晚安不會忘記。 本命年結(jié)束了,沒有想象中的倒霉,可能是我的想象...
    想要起飛的懶癌患者閱讀 288評論 0 1

友情鏈接更多精彩內(nèi)容