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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
.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 |
|
|||
|
"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 |
|
|||
|
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! |
|
|||
|
"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. |
|
|||
|
"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. |
|
|||
|
"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 |