Preg_split - Php Help Required

liunx

Guest
Hi,<br /><br />We have a number of strings, which take on the basic format of fieldname followed by value. Each fieldname always starts with {$ and ends with $}. The fieldname is always made up of alphanumerics and spaces. I need to split the strings by the fieldname, so I have two componets 1. Fieldname 2. Value<br /><br />e.g. strings are:<br /><br />string: {$begin Take$} My story began<br />string: {$a publication$} The daily newspaper<br />string: {$ date of creation$} 04/06/2007<br /><br />- so I would then have the parsed data as:<br />fieldname:{$begin Take} value:My story began<br />fieldname:{$a publication} value:The daily newspaper<br />fieldname:{$ date of creation$} value:04/06/2007<br /><br />We're doing this parsing in PHP and need the code/ help with the preg_split for this.<br />All help very much appreciated<br /><br />Alan<!--content-->
Not sure if preg_split can be used for double splitting like that. If each string is seperate you could loop through them.<br /><br />The regex would be something like '!(\{\$\w\$\}) (.+)!' or something similar.<!--content-->
I don't know if this is the best way to do it or not, but this should work:<br /><br />list( $fieldname, $value ) = preg_split( '/(\\{\\$.+\\$\\})/', $string, 2, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );<!--content-->
 
Back
Top