Hello
I have problem in parsing String using preg_split,
my problem is that i have one string witch is comma (,) seperated, but it should allow comma(,) between double quotes
for ex. string is
ID, Name, add, City
1, "John Methew", "821, Lodi", "NJ"
now i dont want to split 821, Lodi
please anyone can help me or guide me on this problem.
how to parse string using preg_split with double quotes
Moderator: General Moderators
- thomas777neo
- Forum Contributor
- Posts: 214
- Joined: Mon Mar 10, 2003 6:12 am
- Location: Johannesburg,South Africa
- thomas777neo
- Forum Contributor
- Posts: 214
- Joined: Mon Mar 10, 2003 6:12 am
- Location: Johannesburg,South Africa
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
<?php
$str = '1, "e;John Methew"e;, "e;821, Lodi"e;, "e;NJ"e;';
echo var_export($str). "e;\n"e;;
preg_match_all('#^\s*(.*?)\s*(?=,)|,\s*(ї"e;\']?)(.*?)\\2\s*(?=,)|\s*,\s*(ї"e;\']?)(.*?)\\4\s*$#m', $str, $matches);
var_export($matches);
?>Code: Select all
'1, "e;John Methew"e;, "e;821, Lodi"e;, "e;NJ"e;'
array (
0 =>
array (
0 => '1',
1 => ', "e;John Methew"e;',
2 => ', "e;821, Lodi"e;',
3 => ', "e;NJ"e;',
),
1 =>
array (
0 => '1',
1 => '',
2 => '',
3 => '',
),
2 =>
array (
0 => '',
1 => '"e;',
2 => '"e;',
3 => '',
),
3 =>
array (
0 => '',
1 => 'John Methew',
2 => '821, Lodi',
3 => '',
),
4 =>
array (
0 => '',
1 => '',
2 => '',
3 => '"e;',
),
5 =>
array (
0 => '',
1 => '',
2 => '',
3 => 'NJ',
),
)