[ create a new paste ] login | about

Link: http://codepad.org/Cz5bsHkx    [ raw code | output | fork ]

PHP, pasted on Nov 22:
<?php
$str = 'id=map,size=512x512,markers=[[latitude=40.5,longitude=-73.9],[latitude=41.5,longitude=-72.9]]';
$lastStart = 0; // Position where we last cut
$len = strlen( $str);
$openedBraces = 0; // Number of braces opened
$result = array();

for( $i = 0; $i < $len; $i++){
    switch( $str[$i]){
        // Handle opening brace
        case '[':
           $openedBraces++;
           break;

        // Handle closing brace
        case ']':
           $openedBraces--; // You may want to check negative numbers
           break;

        // Handle coma (it's sane operation only if there are no braces opened)
        case ',':
           if( $openedBraces == 0){
               $result[] = substr( $str, $lastStart, $i-$lastStart);
               $lastStart = $i+1;
           }
           break;
    }
}

$result[] = substr( $str, $lastStart);

print_r($result);


Output:
1
2
3
4
5
6
Array
(
    [0] => id=map
    [1] => size=512x512
    [2] => markers=[[latitude=40.5,longitude=-73.9],[latitude=41.5,longitude=-72.9]]
)


Create a new paste based on this one


Comments: