This is a discussion on preg_replace question within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I have a string containing something like this: heading1 information ... ... heading2 information2 ... ... etc - i'm wanting to split this into ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have a string containing something like this:
heading1 information ... ... heading2 information2 ... ... etc - i'm wanting to split this into an array each containing one section. Here's what i'm trying: preg_split('/^\S+/m', $string) - this isn't working. any suggestions? Thanks, -- Fred Emmott (http://www.fredemmott.co.uk) |
|
|||
|
This splits the string as you intended, I'm assuming you want the
headings kept with their respective information sections? I don't think you can do that directly with preg_split, but if you do: $Results = preg_split('/^\S+/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE); $Results will look something like: $Results[0] = "heading1" $Results[1] = "information1..." $Results[2] = "heading2" $Results[3] = "information2..." etc. Now all you need to do is concantenate $Results[0] with $Results[1], $Results[2] with $Results[3], and so on. Hope this helps, Oli Fred Emmott wrote: > I have a string containing something like this: > > heading1 > information > ... > ... > heading2 > information2 > ... > ... > > etc - i'm wanting to split this into an array each containing one section. > Here's what i'm trying: preg_split('/^\S+/m', $string) - this isn't > working. any suggestions? > > Thanks, > |
|
|||
|
Sorry, that should be:
$Results = preg_split('/(^\S+)/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE); Oli Filth wrote: > This splits the string as you intended, I'm assuming you want the > headings kept with their respective information sections? I don't think > you can do that directly with preg_split, but if you do: > > $Results = preg_split('/^\S+/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE); > > $Results will look something like: > > $Results[0] = "heading1" > $Results[1] = "information1..." > $Results[2] = "heading2" > $Results[3] = "information2..." > etc. > > Now all you need to do is concantenate $Results[0] with $Results[1], > $Results[2] with $Results[3], and so on. > > Hope this helps, > Oli > > > Fred Emmott wrote: > >> I have a string containing something like this: >> >> heading1 >> information >> ... >> ... >> heading2 >> information2 >> ... >> ... >> >> etc - i'm wanting to split this into an array each containing one >> section. >> Here's what i'm trying: preg_split('/^\S+/m', $string) - this isn't >> working. any suggestions? >> >> Thanks, >> |