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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:15:34+00:00 2026-06-02T04:15:34+00:00

trying to get two word phrases from an array but I keep getting one

  • 0

trying to get two word phrases from an array but I keep getting one word phrases with a white space either before or after the word.

$text = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $text);
$textarray = explode(" ", $text);
array_filter($textarray);
$textCount = array_count_values($textarray);
arsort($textCount);
$twoWords = array();
for ($i = 0; $i<=count($textarray); ++$i) {
    if ($textarray[$i]<>"  ") {
        $twoWords[$i] = $textarray[$i]." ".$textarray[$i + 1];
    } else {
        $twoWords[$i] = "blah";
    }
}
foreach ($twoWordsCount as $tey => $val2) {
    if ($tey == null || $tey == "" || $tey == "\r\n"  || $tey == "\t"  || $tey == "\r"  || $tey == "\n" || $tey == "&nbsp;" || $tey == "  " || $tey == " ") {
        //do nothing
    } else {
        echo $val2."\n";
    }
}

and for some reason this just returns values likes whitespace Hello or Test and then a whitespace, but I want it to return hello test

  • 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-02T04:15:38+00:00Added an answer on June 2, 2026 at 4:15 am

    No idea what the second half of the script is supposed to do but the first part can be reduced to one preg_split() line of code

    <?php
    foreach( array('hello world', ' hello world', 'hello world ', '  hello     world     ') as $input ) {
        $w = preg_split('!\s+!', $input, -1, PREG_SPLIT_NO_EMPTY);
        var_dump($w);
    }
    

    prints

    array(2) {
      [0]=>
      string(5) "hello"
      [1]=>
      string(5) "world"
    }
    array(2) {
      [0]=>
      string(5) "hello"
      [1]=>
      string(5) "world"
    }
    array(2) {
      [0]=>
      string(5) "hello"
      [1]=>
      string(5) "world"
    }
    array(2) {
      [0]=>
      string(5) "hello"
      [1]=>
      string(5) "world"
    }
    

    edit: maybe you’re looking for something like this

    <?php
    $input = getData();
    $w = preg_split('![\s[:punct:]]+!', $input, -1, PREG_SPLIT_NO_EMPTY);
    $w = array_count_values($w);
    arsort($w);
    $ci = new CachingIterator( new ArrayIterator( $w ) );
    foreach( $ci as $next=>$cnt ) {
        printf("%s(%d) %s(%d)\n",
            $next, $cnt,
            $ci->getInnerIterator()->key(), $ci->getInnerIterator()->current()
        );
    }
    
    
    function getData() {
        return <<< eot
    Mary had a little lamb,
    whose fleece was white as snow.
    
    And everywhere that Mary went,
    the lamb was sure to go.
    
    It followed her to school one day
    which was against the rules.
    
    It made the children laugh and play,
    to see a lamb at school.
    
    And so the teacher turned it out,
    but still it lingered near,
    
    And waited patiently about,
    till Mary did appear.
    
    "Why does the lamb love Mary so?"
    the eager children cry.
    
    "Why, Mary loves the lamb, you know."
     the teacher did reply.
    eot;
    }
    

    which prints

    the(8) lamb(5)
    lamb(5) Mary(5)
    Mary(5) was(3)
    was(3) And(3)
    And(3) to(3)
    to(3) It(2)
    It(2) school(2)
    school(2) so(2)
    so(2) Why(2)
    Why(2) did(2)
    did(2) it(2)
    it(2) teacher(2)
    teacher(2) children(2)
    children(2) a(2)
    a(2) waited(1)
    waited(1) patiently(1)
    patiently(1) about(1)
    about(1) till(1)
    [...]
    white(1) went(1)
    went(1) (0)
    

    see http://docs.php.net/class.cachingiterator

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a timer to count two times but I have
I'm trying to get two types of URL behaviors to work, but I get
Trying to get two checkbox controls to work with each other. Pseudo-code example: If
In C#, I'm trying to get the last two decimal places of a double
In my GWT project, I'm trying to get it so two DialogBoxes can pass
I am trying to get all logfiles between two dates with regular expressions. My
I have two tables: hotels and hotel_rooms I'm trying to get a table with
I've been trying to get a certain Google API to work for two days
I am trying to get at least three words separated by two commas.I have
I'm trying to create two functions: get & set, that both accept an arbitrary

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.