php - Formatted string parsing with conflicting delimiter -
whats quickest/easiest way parse string delimited \ , known keywords?
$str = 'key1\0815\key2\some string here\key3\string \ in between\key4\0 i need have them in array (key => value) later use, how manage split string when values might contain \? keywords known, unique.
explode() fail on , sscanf() hughe amount of manual editing
use preg_split() achieve this:
$arr = preg_split('/\\\\(?!\s)/', $str); $result = array(); foreach (array_chunk($arr, 2) $sub) { list($key, $value) = $sub; $result[$key] = $value; } output:
array ( [keyword1] => 0815 [keyword2] => string here [keyword3] => string \ in between [keyword4] => 0 )
Comments
Post a Comment