Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:40:39+00:00 2026-06-09T03:40:39+00:00

Suppose I have a string, like so: $string = ‘president barack obama’; Now, suppose

  • 0

Suppose I have a string, like so:

$string = 'president barack obama';

Now, suppose I want to explode this into an array, breaking at the words. You’d think I can just use explode(), right? That works, but what if I want an array of all possible left-to-right combinations of the words? Like so:

Array
(
    [0] => 'barack'
    [1] => 'barack obama'
    [2] => 'obama'
    [3] => 'president'
    [4] => 'president barack'
    [5] => 'president barack obama'
)

What is the most efficient way to do this?


Possible solution:

I’ve come up with one possible solution so far, but I’m hoping one of you can give me a better idea. I imagine approaching this like so:

  1. Explode normally.
  2. Loop through each word.
  3. For each word, store it in an array. Then, check if there is another word in the array (after itself). If there is, add a new array value which consists of $current_word . ' ' . $new_word;. Do this for each word.

Now, that will probably work. However, it seems annoying, and I’m afraid someone else may have a better way of doing this. What do you all recommend? Is there, perhaps, a PHP function that does just this that I don’t know about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:40:40+00:00Added an answer on June 9, 2026 at 3:40 am

    This should do what you are looking for:

    $string  = 'president barack obama won';
    $results = getWordPermutaions($string);
    print_r($results);
    
    function getWordPermutaions($inStr) {
      $outArr   = Array();
      $tokenArr = explode(" ", $inStr);
      $pointer  = 0;
      for ($i=0; $i<count($tokenArr); $i++) {
        $outArr[$pointer] = $tokenArr[$i];
        $tokenString = $tokenArr[$i];
        $pointer++; 
        for ($j=$i+1; $j<count($tokenArr); $j++) {
          $tokenString .= " " . $tokenArr[$j];
          $outArr[$pointer] = $tokenString;
          $pointer++;
        }
      }
      return $outArr;
    }
    
    /*
    $results:
    Array (
            [0] => 'president'
            [1] => 'president barack'
            [2] => 'president barack obama'
            [3] => 'president barack obama won'
            [4] => 'barack'
            [5] => 'barack obama'
            [6] => 'barack obama won'
            [7] => 'obama'
            [8] => 'obama won'
            [9] => 'won'
    )
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose I have string like this str1ng for test now i want to check
Suppose I have a string like this: string = Manoj Kumar Kashyap; Now I
Suppose I have a string like this: abc%\%%%% I want to replace multiple %%%
Suppose I have a string like this: blah=-Xms512m I want the output as 512
Suppose I have a string like this: one two three four five six seven
In perl suppose I have a string like 'hello\tworld\n' , and what I want
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
Suppose I have a string like this SOMETHING [1000137c] SOMETHING = John Rogers III
Suppose I have a string of 123456789 . I would like to split this
Suppose I have a body of text (string) like this: str = 'This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.