Jump to content

[SOLVED] trouble with explode("\n",$data);


colbyg

Recommended Posts

i am making a script that uses a flat file db, and i am trying to read a file that has:

http://colbylyn.com/smilie.jpg

 

whenever i use explode to split each line from the file, for some reason the :) has a new line after it. like:

<?php
$data=file_get_contents('./smilies.txt');

$data=explode("\n",$data);

echo '|'.$data[0].'|';

 

this outputs

|
|

 

but if i use a string that holds the contents of the file instead, it works correctly. there is no newline after the :). i am so confused.

 

<?php
$data="
http://colbylyn.com";

$data=explode("\n",$data);

echo '|'.$data[0].'|';

output:

||

Link to comment
https://forums.phpfreaks.com/topic/150624-solved-trouble-with-explodendata/
Share on other sites

If I understand correctly, you have a line break in the ouput, and you don't want it there...?

 

So, you could try this to remove the line break by using str_replace(), like so:

 

$data[0] = str_replace("\n", '', $data[0]);

 

 

i am trying to find out why there is a newline after i used explode().

You would be better to use file to get each line into an array in the first place.

 

i was trying to go for the fastest way to get the data, and i timed file and trimming each line against using file_get_contents and explode and file_get_contents was faster. but i'm just going to use | instead of \n as a separator.

Linux/Unix (and the amazing Amiga) both use just the \n for a new line (0x0d) but Windows uses both \r (0x0a) and \n (0x0d).

 

\r returns to the start of the line

\n advances one line

 

That's why we have two modes of upload - ASCII and binary when uploading to *nix systems, all it does is convert \r\n into just \n.

If you ever view a text file and everything seems to appear on one line or a massive chunk of text at the top of the file it's probably because the text file was downloaded in binary mode and the carriage returns weren't converted. Sometimes it's people trying to put others off from ripping their work but a quick search & replace soon fixes it.

 

Ever wondered why text files seem to be a little smaller after you upload them? It's more noticable with large files. You can upload text in binary mode and it'll work but download it in binary mode and it'll appear a bit messy.

If you ever view a text file and everything seems to appear on one line or a massive chunk of text at the top of the file it's probably because the text file was downloaded in binary mode and the carriage returns weren't converted. Sometimes it's people trying to put others off from ripping their work but a quick search & replace soon fixes it.

 

Ever wondered why text files seem to be a little smaller after you upload them? It's more noticable with large files. You can upload text in binary mode and it'll work but download it in binary mode and it'll appear a bit messy.

 

i always wondered while files were like that when i downloaded them. i always copy and pasted it into a textbox on a browser and it fixed it lol.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.