How can I explode a string by commas, but not where the commas are within this («») quotes?

Valijon Rahimov

I need to explode a string by commas but not within quotes(«»).

I have a string like: "5,test2,4631954,Y,«some, string, text.»,299.00 TJS,http://some-link"

i want this result:

[
 0 => 5,
 1 => test2,
 2 => 4631954,
 3 => Y,
 4 => «some, string, text.»,
 5 => 299.00 TJS,
 6 => http://some-link
]

Tried with preg_split, str_getcsv but didnt get needed result.

$res = str_getcsv($res, ',');
$res = preg_split("/(?<=\)),/", $res);
Wiktor Stribiżew

If you have no regular quotation marks to take care of in your strings to split, you may get what you need with a SKIP-FAIL based regex:

$s = "5,test2,4631954,Y,«some, string, text.»,299.00 TJS,http://some-link";
print_r(preg_split('~«[^«»]*»(*SKIP)(*F)|\s*,\s*~', $s));

See the PHP demo

Pattern details

  • «[^«»]*»(*SKIP)(*F) - «, 0+ chars other than « and », and then » are matched, the regex engine is moved to the end of this matched text, and then the whole text is removed from the current match buffer and the engine goes on to search for the next match
  • | - or
  • \s*,\s* - a , enclosed with 0+ whitespaces

Output:

Array
(
    [0] => 5
    [1] => test2
    [2] => 4631954
    [3] => Y
    [4] => «some, string, text.»
    [5] => 299.00 TJS
    [6] => http://some-link
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove commas or whatever from within a string?

How to Deal double quotes and commas within csv string?

Split string on commas but ignore commas within double-quotes?

Split string on commas but ignore commas within double-quotes?

explode commas but ignore commas within brackets php

How can I split a comma-separated string, ignoring commas inside double quotes and parentheses?

how can I export a list to txt without commas and quotes

Python replace ',' within quotes, but keep other commas, then remove the quotes in a string

How do I parse a .csv file with quotes and commas within the quotes Node.js

AWK: How to remove data where column count is not the same as header count and data has commas within double quotes

how can I keep commas?

Split a string on commas not contained within double-quotes with a twist

When splitting commas, how to ignore commas in quotes?

Split string on commas but ignore commas within double-quotes using shell scripting in a .csv file?

Split a string by commas but ignore commas within double-quotes using Javascript

Javascript/RegEx: Split a string by commas but ignore commas within double-quotes

How can I format a String number to have commas and round?

How can I split a String by all whitespace or commas

How can I add commas after every ] in a string using Python?

How I can get numbers from a String and separates with commas

How I can get positive numbers from a String and separates with commas

how can i remove all commas at the end of the string?

How to explode a string but not within quotes in php?

Split based on commas but ignore commas within double-quotes

How can I make regex pattern with characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &

Explode string on commas and pipes to create two arrays

How to add quotes and commas in powershell

Remove only the commas present within the single quotes

Remove only the commas present within the double quotes