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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:32:42+00:00 2026-06-15T09:32:42+00:00

This code will split the string into an array that contains test and string:

  • 0

This code will split the string into an array that contains test and string:

$str = 'test string';
$arr = preg_split('/\s+/', $str);

But I also want to detect quotes and ignore the text between them when splitting, for example:

$str = 'test "Two words"';

This should also return an array with two elements, test and Two words.

And another form, if possible:

$str = 'test=Two Words';

So if the equal sign is present before any spaces, the string should be split by =, otherwise the other rules from above should apply.

So how can I do this with preg_split?

  • 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-15T09:32:43+00:00Added an answer on June 15, 2026 at 9:32 am

    Try str_getcsv:

    print_r(str_getcsv('test string'," "));
    print_r(str_getcsv('test "Two words"'," "));
    print_r(str_getcsv('test=Two Words',"="));
    

    Outputs

    Array
    (
        [0] => test
        [1] => string
    )
    Array
    (
        [0] => test
        [1] => Two words
    )
    Array
    (
        [0] => test
        [1] => Two Words
    )
    

    You can use something like preg_match to check if there’s an equal sign exist before space and then determine what delimiter to use.

    Works only in PHP>=5.3 though.

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

Sidebar

Related Questions

I am trying to create a function that will split a string into search
I have this code void split(vector<float> &fvec, string str) { int place = 0;
I have this code Dim parts As New List(Of String)(Regex.Split(RichTextBox2.Text, ~\d)) That splits lines
I have the following code (inherited!) that will split a line of text into
this code will not pass the query string to default3.aspx on Image Button ..
I have a string that contains comma seperated email addresses. I then load this
I want to split a string like My dog into an array of: |
Is there a regex that would work with String.split() to break a String into
This code will get us all the properties of a class: Dim myPropertyInfo As
This code will output some blinks on divs when we do the mouse over.

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.