473,586 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
6 23928
.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
Jul 17 '05 #2
"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
Jul 17 '05 #3
Michael Fesser <ne*****@gmx.ne t> wrote in message news:<cg******* *************** **********@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!
Jul 17 '05 #4
"William Krick" <wk****@gmail.c om> wrote in message
news:c0******** *************** ***@posting.goo gle.com...
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_E MPTY);

A regular expression split is safer than explode in this case, since you're
using a newline character as the delimiter.
Jul 17 '05 #5
"Chung Leong" <ch***********@ hotmail.com> wrote in
news:W7******** ************@co mcast.com:
"William Krick" <wk****@gmail.c om> wrote in message
news:c0******** *************** ***@posting.goo gle.com...
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_E MPTY);

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.
Jul 17 '05 #6
"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
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10178
by: Richard Lawrence | last post by:
Hi there, I'm having a problem with PHP which I'm not sure how to best solve. Here is the code: $fp = fopen("comments.txt", "r"); while(!feof($fp)) { $line = fgets($fp, 1024); list($key, $value) = explode(":", $line, 2);
5
10792
by: Lyle A. Sieren | last post by:
I posted this earlier with another question but did not explain myself well... How do get a line feed character on the end of a line using the streamwriter.writeline fuction? Following are a list of possible terminators. I need to use the line feed, option 2, instead of the others for the application that will be reading the file. ...
4
3124
by: Fuzzyman | last post by:
Hello all, I'm handling some text files where I don't (necessarily) know the encoding beforehand. Because I use regular expressions to parse the text I *must* decode UTF16 encoded text (otherwise the regexes split on byte boundaries). I can recognise UTF8 and BOM and remove (but not necessarily decode). For UTF16 it seems that the Python...
4
58499
by: Water Cooler v2 | last post by:
Besides embedding <BR/>, like general purpose programming languages, I thought you could embed escape sequences such as \n or \r\n into string arguments to cause line breaks in JavaScript. To test, I tried this: <BODY> <BR/><BR/>
6
10378
by: Stick | last post by:
Hi, I am trying to start my email app (in this case Eudora) with a programatically generated email. However, Eudora does not seem to recognize any of the ways I have tried to create line breaks. I have tried "\n", "\r\n", and using the StringBuilder AppendLine() method like this: StringBuilder body = new StringBuilder(1024);
2
3376
by: Jim | last post by:
Hello, I need a program that will traverse a directory tree to ensure that there are unix-style line endings on every file in that tree that is a text file. To tell text files from others I want to use the unix "file" command (Python's "mimetypes" is not so good for me). But I am stuck on something about getting that output, and I'd...
16
2935
by: mazwolfe | last post by:
Someone recently asked about reading lines. I had this code written some time ago (part of a BASIC-style interpreter based on H. Shildts in Art of C) to read a file with the lines ended in any format: Microsoft-style CR/LF pair, Unix-style NL, or Mac-style CR. It also allows for EOF that does not follow a blank line. I thought this would...
9
11036
by: antar2 | last post by:
I am a starter in python and would like to write a program that reads lines starting with a line that contains a certain word. For example the program starts reading the program when a line is encountered that contains 'item 1' The weather is nice Item 1 We will go to the seaside ....
9
5417
by: mijn naam | last post by:
Hi, I tried searching myself using google and on php.net, but too much irrelevant answers and not a single one helpful so please bare with me. Could someone please show me how to get one line from a file, much like unix "grep '^needle' haystack" ? I'm sure there are zero or one matches, and I don't need fancy regular expressions, just...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.