preg_replace question

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-28-2004
Fred Emmott
 
Posts: n/a
Default preg_replace question

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)
Reply With Quote
  #2 (permalink)  
Old 10-28-2004
Oli Filth
 
Posts: n/a
Default Re: preg_replace question

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,
>

Reply With Quote
  #3 (permalink)  
Old 10-28-2004
Oli Filth
 
Posts: n/a
Default Re: preg_replace question

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,
>>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:23 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0