php - How to parse column separated key-value text with possible multiline strings -
i need parse following text:
first: 1 second: 2 multiline: blablablabla bla2bla2bla2 bla3b , key: value in middle if strting fourth: value
value string or multiline string, @ same time value contain "key: blablabla" substring. such subsctring should ignored (not parsed separate key-value pair).
please me regex or other algorithm.
ideal result be:
$regex = "/some regex/"; $matches = []; preg_match_all($regex, $html, $matches); // $mathes has key , value parsed pairs, including multilines values
thank you.
i tried simple regexes result incorrect, because don't know how handle multilines:
$regex = "/(.+?): (.+?)/"; $regex = "/(.+?):(.+?)\n/"; ...
you can pattern:
$pattern = '~(?<key>[^:\s]+): (?<value>(?>[^\n]*\r)*?[^\n]*)(?=\r\s+:|$)~'; preg_match_all($pattern, $txt, $matches, preg_set_order); print_r($matches);
Comments
Post a Comment