Jump to content

[SOLVED] Splitting Strings


cs1h

Recommended Posts

$section = array();
$sections = array();
$string = 'x,1,3,42,2,5,7,y,x,1,8,4,3,y,x,5,3,3,4,y,x,2,8,7,4,6,y';
$parts = explode(',', $string);
foreach ($parts as $part) {
    if ('x' === $part && !empty($section)) {
        $sections[] = implode(',', $section);
        $section = array();
    }
    $section[] = $part;
}

print_r($sections);

 

Outputs:

 

Array ( [0] => x,1,3,42,2,5,7,y [1] => x,1,8,4,3,y [2] => x,5,3,3,4,y ) 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.