Useing preg_split at 12 commas in string? can it be done?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
BrettCarr
Forum Newbie
Posts: 22
Joined: Fri Feb 12, 2010 6:45 pm

Useing preg_split at 12 commas in string? can it be done?

Post 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?? :crazy:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Useing preg_split at 12 commas in string? can it be done?

Post 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! :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Useing preg_split at 12 commas in string? can it be done?

Post by John Cartwright »

Grr... you already have a topic open on this.

Topic Locked.
Locked