PHP explode and line terminators

This is a discussion on PHP explode and line terminators within the PHP Language forums, part of the PHP Programming Forums category; I have a string containing concatenated ascii "records" that are each terminated by '\n'. For testing purposes, I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-27-2004
William Krick
 
Posts: n/a
Default PHP explode and line terminators

I have a string containing concatenated ascii "records" that are each
terminated by '\n'.

For testing purposes, I construct sample data like this...

$mystring = "a|b|c|d\n"."e|f|g|h\n"."i|j|k|l\n"."m|n|o|p\n ";


I want to use explode() to put the records into an array like this...

$records = explode("\n", $mystring);

However when I do this, the resulting $records array contains one
extra empty element that I assume is caused by the final terminating
'\n'

Is there some slick way to solve this problem?

Is it possible that "explode" isn't the right tool for the job?

....
Krick
Reply With Quote
  #2 (permalink)  
Old 10-27-2004
Michael Fesser
 
Posts: n/a
Default Re: PHP explode and line terminators

.oO(William Krick)

>$records = explode("\n", $mystring);
>
>However when I do this, the resulting $records array contains one
>extra empty element that I assume is caused by the final terminating
>'\n'


Correct.

>Is there some slick way to solve this problem?


$records = explode("\n", trim($mystring));

Micha
Reply With Quote
  #3 (permalink)  
Old 10-28-2004
steve
 
Posts: n/a
Default Re: Re: PHP explode and line terminators

"Michael Fesser" wrote:
> .oO(William Krick)
>
> >$records = explode("n", $mystring);
> >
> >However when I do this, the resulting $records array contains

> one
> >extra empty element that I assume is caused by the final

> terminating
> >'n'

>
> Correct.
>
> >Is there some slick way to solve this problem?

>
> $records = explode("n", trim($mystring));
>
> Micha


do a preg_replace on the string (there are faster ways but this works
anyways)

$mystring = preg_replace("/\n$/", ’’, $mystring);

and then explode it.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-explode-...ict164939.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=552896
Reply With Quote
  #4 (permalink)  
Old 10-28-2004
William Krick
 
Posts: n/a
Default Re: PHP explode and line terminators

Michael Fesser <netizen@gmx.net> wrote in message news:<cguvn0dfm8d8fj3igeh8b0kqu358rakamk@4ax.com>. ..
> .oO(William Krick)
>
> >$records = explode("\n", $mystring);
> >
> >However when I do this, the resulting $records array contains one
> >extra empty element that I assume is caused by the final terminating
> >'\n'

>
> Correct.
>
> >Is there some slick way to solve this problem?

>
> $records = explode("\n", trim($mystring));
>
> Micha



BINGO! you da man. Thanks!
Reply With Quote
  #5 (permalink)  
Old 10-28-2004
Chung Leong
 
Posts: n/a
Default Re: PHP explode and line terminators

"William Krick" <wkrick@gmail.com> wrote in message
news:c08557e4.0410271124.6b336b66@posting.google.c om...
> I have a string containing concatenated ascii "records" that are each
> terminated by '\n'.
>
> For testing purposes, I construct sample data like this...
>
> $mystring = "a|b|c|d\n"."e|f|g|h\n"."i|j|k|l\n"."m|n|o|p\n ";
>
>
> I want to use explode() to put the records into an array like this...
>
> $records = explode("\n", $mystring);
>
> However when I do this, the resulting $records array contains one
> extra empty element that I assume is caused by the final terminating
> '\n'
>
> Is there some slick way to solve this problem?
>
> Is it possible that "explode" isn't the right tool for the job?
>
> ...
> Krick


$records = preg_split('/[\r\n]+/', $mystring, -1, PREG_SPLIT_NO_EMPTY);

A regular expression split is safer than explode in this case, since you're
using a newline character as the delimiter.


Reply With Quote
  #6 (permalink)  
Old 10-28-2004
Theo
 
Posts: n/a
Default Re: PHP explode and line terminators

"Chung Leong" <chernyshevsky@hotmail.com> wrote in
news:W7udnZdJaesE4B3cRVn-vw@comcast.com:

> "William Krick" <wkrick@gmail.com> wrote in message
> news:c08557e4.0410271124.6b336b66@posting.google.c om...
>> I have a string containing concatenated ascii "records" that are each
>> terminated by '\n'.
>>
>> For testing purposes, I construct sample data like this...
>>
>> $mystring = "a|b|c|d\n"."e|f|g|h\n"."i|j|k|l\n"."m|n|o|p\n ";
>>
>>
>> I want to use explode() to put the records into an array like this...
>>
>> $records = explode("\n", $mystring);
>>
>> However when I do this, the resulting $records array contains one
>> extra empty element that I assume is caused by the final terminating
>> '\n'
>>
>> Is there some slick way to solve this problem?
>>
>> Is it possible that "explode" isn't the right tool for the job?
>>
>> ...
>> Krick

>
> $records = preg_split('/[\r\n]+/', $mystring, -1,
> PREG_SPLIT_NO_EMPTY);
>
> A regular expression split is safer than explode in this case, since
> you're using a newline character as the delimiter.
>
>
>


maybe also use trim? it also automatically gets rid of all kinds of
things at the end of a string (newlines, spaces etc), or you can use
something specific if you prefer.
Reply With Quote
  #7 (permalink)  
Old 11-04-2004
steve
 
Posts: n/a
Default Re: Re: PHP explode and line terminators

"steve" wrote:
> trim would only remove spaces, and not "\n"
>
> do a preg_replace on the string (there are faster ways but this

works
> anyways)
>
> $mystring = preg_replace("/\n$/", ’’, $mystring);
>
> and then explode it.


My mistake. Trim does trim all the other characters besides white
space
http://ca3.php.net/trim

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-explode-...ict164939.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=560127
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 08:01 AM.


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