dongpo8702 2014-07-26 08:27
浏览 54
已采纳

php根据带有空格分隔符的给定字符串生成多个字符串

How to generate multiple string base on given string with space separator. Ex.: I have one string like

Ex.:1 'This is string' will output following strings: 'This is', 'This string', 'is string'

$arrStrSplit = explode($splitBy, $strInfo);
for($outer = 0; $outer < count($arrStrSplit); $outer++)
{
    for($inner = $outer+1; $inner < count($arrStrSplit); $inner++)
    {
        array_push($arrStrSplitPair, $arrStrSplit[$outer].' '.$arrStrSplit[$inner]);
    }
}

What if have multiple word string more than 3+

Ex.2: 'This is new string'

  Output:

  'This is', 'This new', 'This string', 'is new', 'is string', 'new string',
  'This is new', 'This is string', 'This new string', 'is new string'

so on : Input string have any no. of words. From 2 to 15-20 etc...

i.e. No. of word string from 2 TO (No. of word of given string - 1)

  • 写回答

1条回答 默认 最新

  • duandie5707 2014-07-26 09:07
    关注

    After many updates...

    http://php.net/manual/en/function.shuffle.php#88408

    $string = 'This is new string';
    $array = explode(' ', $string);
    
    function powerSet($in, $minLength = 1) {
        $count = count($in);
        $members = pow(2,$count);
        $return = array();
        for ($i = 0; $i < $members; $i++) {
            $b = sprintf("%0".$count."b",$i);
            $out = array();
            for ($j = 0; $j < $count; $j++) {
                if ($b{$j} == '1') $out[] = $in[$j];
            }
            if (count($out) >= $minLength) {
                $return[] = $out;
            }
        }
        return $return;
    } 
    $sets = powerSet($array, 1);
    

    That's all I think! Best regards!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀