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 ) 

demo


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -