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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:36:10+00:00 2026-06-09T09:36:10+00:00

Further on from my previous question about preg_split which was answers super fast, thanks

  • 0

Further on from my previous question about preg_split which was answers super fast, thanks to nick; I would really like to extend the scenario to no split the string when a delimiter is within quotes. For example:

If I have the string foo = bar AND bar=foo OR foobar="foo bar", I’d wish to split the sting on every space or = character but include the = character in the returned array (which works great currently), but I don’t want to split the string either of the delimiters are within quotes.

I’ve got this so far:

<!doctype html>
<?php

$string = 'foo = bar AND bar=foo';

$array = preg_split('/ +|(=)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

?>
<pre>
<?php

print_r($array);

?>
</pre>

Which gets me:

Array
(
    [0] => foo
    [1] => =
    [2] => bar
    [3] => AND
    [4] => bar
    [5] => =
    [6] => foo
)

But if I changed the string to:

$string = 'foo = bar AND bar=foo OR foobar = "foo bar"';

I’d really like the array to be:

Array
(
    [0] => foo
    [1] => =
    [2] => bar
    [3] => AND
    [4] => bar
    [5] => =
    [6] => foo
    [6] => OR
    [6] => foobar
    [6] => =
    [6] => "foo bar"
)

Notice the "foo bar" wasn’t split on the space because it’s in quotes?

Really not sure how to do this within the RegEx or if there is even a better way but all your help would be very much appreciated!

Thank you all in advance!

  • 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-09T09:36:12+00:00Added an answer on June 9, 2026 at 9:36 am

    I was able to do this by adding quoted strings as a delimiter a-la

    "(.*?)"| +|(=)
    

    The quoted part will be captured. It seems like this is a bit tenuous and I did not test it extensively, but it at least works on your example.

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

Sidebar

Related Questions

This question follows on from a previous question, that has raised a further issue.
Following on from a previous question in which I asked: How can I use
It is a question build upon the previous question (http://stackoverflow.com/questions/6538448/r-how-to-write-a-loop-to-get-a-matrix). It is different from
This code is taken from a previous question, but my question directly relates to
I'm quoting myself on a previous question I asked to further explain: I'm trying
This is a followup to my previous question. Parsing file names from a character
To revisit a previous question with a further stipulation... Anyone know how to do
I'm following on from a previous question . The answer I accepted involves using
I'm writing a Swing application, and further to my previous question , have settled
Further to my previous question, I have a jQuery event that I want to

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.