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 169513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:38:38+00:00 2026-05-11T12:38:38+00:00

I have a sentence, for example John Doe moved to New York last year.

  • 0

I have a sentence, for example

John Doe moved to New York last year.

Now I split the sentence into the single words and I get:

array(‘John’, ‘Doe’, ‘moved’, ‘to’, ‘New’, ‘York’, ‘last’, ‘year’)

That’s quite easy. But then I want to combine the single words to get all the composed terms. It doesn’t if the composed terms make sense, I want to get all of them, though. The result of that operation should look like this:

John, Doe, John Doe, moved, Doe moved, John Doe moved, to, moved to, Doe moved to …

The words should be composed to terms to a limit of k parts. In the example above, the limit is 3. So a term can contain 3 words at most.

The problem: How could I code the composition in PHP? It would be great if I had a function which gets a sentence as the input and gives an array with all terms as the output.

I hope you can help me. Thanks in advance!

  • 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. 2026-05-11T12:38:39+00:00Added an answer on May 11, 2026 at 12:38 pm

    If you already have the code for splitting the words into an array, this function will let you select the longest you wish your phrases to be, and return to you an array of arrays containing your phrases.

    function getPhrases($array, $maxTerms = 3) {     for($i=0; $i < $maxTerms; $i++) { //Until we've generated terms of all lengths          for($j = 0; $j < (sizeof($array) - $i); $j++) { //Until we've iterated as far through the array as we should go              $termArray[] = array(array_slice($array, $j, ($i+1))); //Add this part of the array to the array          }     }     return $termArray; }  //Usage example  $newarray = explode(' ', 'This is a pretty long example sentence'); print_r(getPhrases($newarray));
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have one sentence (several words without a dot after the last one).
I have this regex on mongodb query to match words by prefix: {sentence: new
I have a sentence in the DB for example: His $name$ is $Luis$ Now,
I want to parse a sentence into words but some sentences have two words
I have the following sentence #bb John can #20 jiang stone [voila] I want
I have to split the word in a sentence which contains more than 40
I have a collection of IEnumerable<sentence> (sentence = string) I want to split all
I have a sentence say This is my new program I want to convert
I have problem in my split Sentence function . The idea of ​​my function
I have a sentence for example hello this is hello stackoverflow hello. What I

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.