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

  • Home
  • SEARCH
  • 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 7814711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:16:49+00:00 2026-06-02T05:16:49+00:00

I wish to split a text string into words separated by spaces. I use

  • 0

I wish to split a text string into words separated by spaces.
I use

$words=explode(" ", $text);

Unfortunately, this method doesn’t work well for me, because I want to know how many spaces are in between.

Is there any better way to do that than going through the whole $text, symbol by symbol, using while statement to fill out $spaces ( $spaces=array(); ) with integers (number of spaces, in most cases it is 1) and read text into $words=array() symbol by symbol?

Here is an additional explanation.

$text="Hello_world_____123"; //symbol "_" actually means a space

Needed:

$words=("Hello","world","123");
$spaces=(1,5);
  • 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-02T05:16:51+00:00Added an answer on June 2, 2026 at 5:16 am

    There are many ways to do what you’re trying to do, but I would probably choose a combination of preg_split() and array_map():

    $text = 'Hello world     123';
    $words = preg_split('/\s+/', $text, NULL, PREG_SPLIT_NO_EMPTY);
    $spaces = array_map(function ($sp) {
        return strlen($sp);
    }, preg_split('/\S+/', $text, NULL, PREG_SPLIT_NO_EMPTY));
    
    var_dump($words, $spaces);
    

    Output:

    array(3) {
      [0]=>
      string(5) "Hello"
      [1]=>
      string(5) "world"
      [2]=>
      string(3) "123"
    }
    array(2) {
      [0]=>
      int(1)
      [1]=>
      int(5)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to use regular expressions to split words into groups of (vowels, not_vowels,
Hi I am having string called 'santhosh'. I wish to split this text as
Let's say we have a long string we wish to split into strings 64
I wish to split text into sentences. Can anyone help me? I also need
I wish to: Reading in two files Split the files into individual strings Compare
I wish to known if exist a clean way to split a string using
I am working on java string split. I wish to split the String according
How do you split a long piece of text into separate lines? Why does
I am receiving a string via query string and I wish to add this
I wish to convert a single string with multiple delimiters into a key=>value hash

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.