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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:55:58+00:00 2026-05-23T02:55:58+00:00

I can find repeated words in text with this function: $str = ‘bob is

  • 0

I can find repeated words in text with this function:

$str = 'bob is a good person. mary is a good person. who is the best? are you a good person? bob is the best?';
    function repeated($str)
    {
        $str=trim($str);  
        $str=ereg_replace('[[:space:]]+', ' ',$str);  
        $words=explode(' ',$str);  
        foreach($words as $w)  
        {  
        $wordstats[($w)]++;  
        }  
        foreach($wordstats as $k=>$v)  
        {  
            if($v>=2)  
            {  
                print "$k"." , ";  
            }  
        }  
    }

thats result me like :

bob , good , person , is , a , the , best?

Q : how i can get result repeated words and Multi-part words between space look like :

bob , good , person , is , a , the , best? , good person , is a , a good , is the , bob is
  • 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-05-23T02:55:59+00:00Added an answer on May 23, 2026 at 2:55 am
    <?php
    $str = 'bob is a good person. mary is a good person. who is the best? are you a good person? bob is the best?';
    
    //all words:
    $found = str_word_count(strtolower($str),1);
    //get all words with occurance of more then 1
    $counts = array_count_values($found);
    $repeated = array_keys(array_filter($counts,function($a){return $a > 1;}));
    //begin results with the groups of 1 word.
    $results = $repeated;
    while($word = array_shift($found)){
        if(!in_array($word,$repeated)) continue;
        $additions = array();
        while($add = array_shift($found)){
            if(!in_array($add,$repeated)) break;
            $additions[] = $add;
            $count = preg_match_all('/'.preg_quote($word).'\W+'.implode('\W+',$additions).'/si',$str,$matches);
            if($count > 1){
                $newmatch = $word.' '.implode(' ',$additions);
                if(!in_array($newmatch,$results)) $results[] = $newmatch;
            } else {
                break;
            }
        }
        if(!empty($additions)) array_splice($found,0,0,$additions);
    }
    var_dump($results);
    

    Yields:

    array(17) {
      [0]=>
      string(3) "bob"
      [1]=>
      string(2) "is"
      [2]=>
      string(1) "a"
      [3]=>
      string(4) "good"
      [4]=>
      string(6) "person"
      [5]=>
      string(3) "the"
      [6]=>
      string(4) "best"
      [7]=>
      string(6) "bob is"
      [8]=>
      string(4) "is a"
      [9]=>
      string(9) "is a good"
      [10]=>
      string(16) "is a good person"
      [11]=>
      string(6) "a good"
      [12]=>
      string(13) "a good person"
      [13]=>
      string(11) "good person"
      [14]=>
      string(6) "is the"
      [15]=>
      string(11) "is the best"
      [16]=>
      string(8) "the best"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is likely something easy to accomplish, but I'm having difficulty even articulating clearly,
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
The only forums I know of are: - WPF Disciples ; this is a
I am using nodejs and the node-mysql driver. I find myself doing repetitive boiler
I have an array with values $somearray = array('car','bike','legs,'car'). I'd like to find out
I'm sorry for the specificity of this question, but i've been up for 48h
I have a page on my ASP.NET site that has a Repeater control to
I have a set of model objects that have a public IsVisible boolean property.
I'm using the term partial to refer to a small section of presentational code
In an array with integers between 1 and 1,000,000 or say some very larger

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.