detect new line in php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

detect new line in php?

Post by potato »

Hi everybody,

i got a text-file wich i want to explode.
How can i detect a new line int the text file?

text look like this:

line1line1line1line1line1line1line1
line2line2line2line2line2line2line2
line3line3line3line3line3line3line3
etc...

so there's no /n or something, just plain text

many thanx in advance
tom
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If there isn't a \n then there isn't a new line...

Do the lines have a fixed width?
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

nope, just a text file with some text.
this is my explode code:

Code: Select all

include 'testfeed.php';
$pizza = explode("/n", $text); //opdelen
$aantal = count($pizza); //aantal pizzas tellen
in the testfeed.php file i have put the whole text in $text.

My result is the whole text itself.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh.. backslash.. not forward slash..
trdesigns
Forum Newbie
Posts: 9
Joined: Wed Aug 03, 2005 11:36 am
Location: Canada

Post by trdesigns »

If I'm not mistaken, the text-file that you have will be interpreted as follows:

Code: Select all

line1line1line1line1line1line1line1\n 
line2line2line2line2line2line2line2\n
line3line3line3line3line3line3line3\n
So you should have no trouble using the split() function:

Code: Select all

$file_contents=file_get_contents("file.txt");
$file_lines=split("/\n",$file_contents);
The above will get you an array $file_lines with the lines as elements

Hope this helps. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not just use file() ??
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

thankyou ;)
Post Reply