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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:51:01+00:00 2026-05-26T16:51:01+00:00

How can I break a string up by spaces in PHP, respecting quoted sub

  • 0

How can I break a string up by spaces in PHP, respecting quoted sub strings? I think there must be some command to parse this type of string like arguments but I don’t know what that is.

Example string:

$string = '12345 abcd "hello world" defgh "nice to meet you" 34554';

I normally use one of these three to break up strings. But I don’t think they have the capability to break the string up like I’m suggesting.

$result = str_replace(' ',"\n",$string);
$result = explode(' ',$string);
$result = preg_replace('#\s#',"\n",$string);

Outcome:

12345
abcd
"hello
world"
defgh
"nice
to
meet
you"
34554

Desired outcome:

12345
abcd
hello world
defgh
nice to meet you
34554

UPDATE

I guess I was impressed with PHP’s parse_str command to deal with URL query variables and was hoping PHP had something I that could work for this example, sort of like getopt does for the command line arguments.

  • 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-26T16:51:02+00:00Added an answer on May 26, 2026 at 4:51 pm

    You can use fgetcsv() / str_getcsv() for this.

    <?php
    $string = '12345 abcd "hello world" defgh "nice to meet you" 34554';
    $row = str_getcsv($string, ' ');
    var_dump($row);
    

    prints

    array(6) {
      [0]=>
      string(5) "12345"
      [1]=>
      string(4) "abcd"
      [2]=>
      string(11) "hello world"
      [3]=>
      string(5) "defgh"
      [4]=>
      string(16) "nice to meet you"
      [5]=>
      string(5) "34554"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i can make drawtext break a string if the string has spaces, or
Is there an existing class in C# that can convert Quoted-Printable encoding to String
How can I make this work? switch(property.PropertyType){ case typeof(Boolean): //doStuff break; case typeof(String): //doOtherStuff
I am wondering how I can break up my index.php homepage to multiple php
If I have a very long string (without spaces!). Can I force the browser
How can I compare 2 strings in C# ignoring the case, spaces and any
How can I replace Line Breaks within a string in C#?
In HTML in the td of a table you can break text by using
How can I break SP execution in an Exception like fashion in MySQL? I
Can someone please break down what a delegate is into a simple, short and

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.