Jump to content
alan_g

Preg_split - Php Help Required

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 );

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...