Code Comments
Programming Forum and web based access to our favorite programming groups.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)
Post Follow-up to this messageThis 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,
>
Post Follow-up to this messageSorry, 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:
>
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.