Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An explode() function that ignores characters inside quotes?

Tags:

string

php

Does somebody know a quick and easy explode() like function that can ignore splitter characters that are enclosed in a pair of arbitrary characters (e.g. quotes)?

Example:

my_explode(
  "/", 
  "This is/a string/that should be/exploded.//But 'not/here',/and 'not/here'"
);

should result in an array with the following members:

This is
a string 
that should be 
exploded.

But 'not/here', 
and 'not/here'

the fact that the characters are wrapped in single quotes would spare them from being splitters.

Bonus points for a solution that can deal with two wrapper characters

(not/here)

A native PHP solution would be preferred, but I don't think such a thing exists!

like image 859
Pekka Avatar asked Jul 16 '10 12:07

Pekka


1 Answers

str_getcsv($str, '/')

There's a recipe for <5.3 on the linked page.

like image 173
Ignacio Vazquez-Abrams Avatar answered Oct 12 '22 11:10

Ignacio Vazquez-Abrams