Page 1 of 1
Useing preg_split at 12 commas in string? can it be done?
Posted: Mon Mar 15, 2010 10:49 pm
by BrettCarr
Im trying to split up a string at every 12 commas in a comma delimeted string here is the string
$stuff = "2010-03-16,852828,643,New,85285,Toll,2,8528528,32,202,852852,ffffgdesddf,sdfsdfgsdfs,1970-01-01,852582,612,New,85286,Toll,2,8528529,30,74,456564,dcscds,sc"
Or when i experience a string that looks like a date
Here is what im tring to use, can this be done or am i dreamin again I gave up one tryin to to it by date lol
Code: Select all
$keywords = preg_split("/([a-zA-Z0-9_-]+?)([,]{12})/", $stuff);
var_dump($keywords);
Is what ive got here completly wrong or am i on the right track??
Re: Useing preg_split at 12 commas in string? can it be done?
Posted: Mon Mar 15, 2010 11:32 pm
by John Cartwright
I'm not exactly sure how you would accomplish this with regex alone, however, you could split the string on all commas and reassemble it back afterward. I.e.,
Code: Select all
$parts = array_map(create_function('$e', 'return implode(",", $e);'), array_chunk(explode(',', $stuff), 13));
//or verbose
Code: Select all
$rawparts = explode(',', $stuff);
$chunkedparts = array_chunk($rawparts, 13);
$parts = array_map(create_function('$e', 'return implode(",", $e);'), $chunkedparts);
Wheres the regex gurus when ya need em!

Re: Useing preg_split at 12 commas in string? can it be done?
Posted: Mon Mar 15, 2010 11:41 pm
by John Cartwright
Grr... you already have a topic open on this.
Topic Locked.