Connecting Tech Pros Worldwide Help | Site Map

PHP explode and line terminators

  #1  
Old July 17th, 2005, 10:20 AM
William Krick
Guest
 
Posts: n/a
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
  #2  
Old July 17th, 2005, 10:20 AM
Michael Fesser
Guest
 
Posts: n/a

re: PHP explode and line terminators


.oO(William Krick)
[color=blue]
>$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'[/color]

Correct.
[color=blue]
>Is there some slick way to solve this problem?[/color]

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

Micha
  #3  
Old July 17th, 2005, 10:20 AM
steve
Guest
 
Posts: n/a

re: PHP explode and line terminators


"Michael Fesser" wrote:[color=blue]
> .oO(William Krick)
>[color=green]
> >$records = explode("n", $mystring);
> >
> >However when I do this, the resulting $records array contains[/color]
> one[color=green]
> >extra empty element that I assume is caused by the final[/color]
> terminating[color=green]
> >'n'[/color]
>
> Correct.
>[color=green]
> >Is there some slick way to solve this problem?[/color]
>
> $records = explode("n", trim($mystring));
>
> Micha[/color]

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
  #4  
Old July 17th, 2005, 10:20 AM
William Krick
Guest
 
Posts: n/a

re: PHP explode and line terminators


Michael Fesser <netizen@gmx.net> wrote in message news:<cguvn0dfm8d8fj3igeh8b0kqu358rakamk@4ax.com>. ..[color=blue]
> .oO(William Krick)
>[color=green]
> >$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'[/color]
>
> Correct.
>[color=green]
> >Is there some slick way to solve this problem?[/color]
>
> $records = explode("\n", trim($mystring));
>
> Micha[/color]


BINGO! you da man. Thanks!
  #5  
Old July 17th, 2005, 10:20 AM
Chung Leong
Guest
 
Posts: n/a

re: PHP explode and line terminators


"William Krick" <wkrick@gmail.com> wrote in message
news:c08557e4.0410271124.6b336b66@posting.google.c om...[color=blue]
> 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[/color]

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


  #6  
Old July 17th, 2005, 10:21 AM
Theo
Guest
 
Posts: n/a

re: PHP explode and line terminators


"Chung Leong" <chernyshevsky@hotmail.com> wrote in
news:W7udnZdJaesE4B3cRVn-vw@comcast.com:
[color=blue]
> "William Krick" <wkrick@gmail.com> wrote in message
> news:c08557e4.0410271124.6b336b66@posting.google.c om...[color=green]
>> 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[/color]
>
> $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.
>
>
>[/color]

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.
  #7  
Old July 17th, 2005, 10:25 AM
steve
Guest
 
Posts: n/a

re: PHP explode and line terminators


"steve" wrote:[color=blue]
> trim would only remove spaces, and not "\n"
>
> do a preg_replace on the string (there are faster ways but this[/color]
works[color=blue]
> anyways)
>
> $mystring = preg_replace("/\n$/", ’’, $mystring);
>
> and then explode it.[/color]

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
Closed Thread