Home > Archive > PHP Language > October 2004 > preg_replace question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
preg_replace question
|
|
| Fred Emmott 2004-10-28, 3:57 pm |
| 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)
| |
| Oli Filth 2004-10-28, 3:57 pm |
| 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,
>
| |
| Oli Filth 2004-10-28, 3:57 pm |
| Sorry, that should be:
$Results = preg_split('/(^\S+)/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
Oli Filth wrote:[color=darkred]
> 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:
>
|
|
|
|
|