alan_g Posted June 4, 2007 Share Posted June 4, 2007 Hi, 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 e.g. strings are: string: {$begin Take$} My story began string: {$a publication$} The daily newspaper string: {$ date of creation$} 04/06/2007 - so I would then have the parsed data as: fieldname:{$begin Take} value:My story began fieldname:{$a publication} value:The daily newspaper fieldname:{$ date of creation$} value:04/06/2007 We're doing this parsing in PHP and need the code/ help with the preg_split for this. All help very much appreciated Alan Quote Link to comment Share on other sites More sharing options...
carbonize Posted June 10, 2007 Share Posted June 10, 2007 Not sure if preg_split can be used for double splitting like that. If each string is seperate you could loop through them. The regex would be something like '!(\{\$\w\$\}) (.+)!' or something similar. Quote Link to comment Share on other sites More sharing options...
click Posted June 10, 2007 Share Posted June 10, 2007 I don't know if this is the best way to do it or not, but this should work: list( $fieldname, $value ) = preg_split( '/(\\{\\$.+\\$\\})/', $string, 2, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.