Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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)

Report this thread to moderator Post Follow-up to this message
Old Post
Fred Emmott
10-28-04 08:57 PM


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

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
10-28-04 08:57 PM


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

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
10-28-04 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:19 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.