Reputation: 23
I need to separate fields on a SQL
query like:
$fields="id, name, contact(name,surname) as full_name,age";
by using preg_split("/\,/",$fields)
result is:
id
name
contact(name
surname)
age
I need to get this result:
id
name
contact(name,surname) as full_name
age
I know the trick is on the regular expression, which are always difficult for me. Could you please help me in the correct regexp?
Upvotes: 0
Views: 531
Reputation: 889
Although Dwza solution does indeed work, a better regexp would be
/(?:,\s*)(?=(?:[^)]|\([^(]*\))*$)/
This regexp capture all of the commas, that may or may not have a space after and that are not inside parenthesis.
Upvotes: 2
Reputation: 6565
solution is
$fields="id, name, contact(name,surname) as full_name, age";
// space needed after the comma ^
than use
preg_split("/\,\s/",$fields)
Upvotes: 0