colbyg Posted March 22, 2009 Share Posted March 22, 2009 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: || Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 expode() is supposed to "destroy" the character you define, ie \n. However, you could try something like $str = str_replace("\n", '', $str); maybe... Quote Link to comment Share on other sites More sharing options...
colbyg Posted March 22, 2009 Author Share Posted March 22, 2009 expode() is supposed to "destroy" the character you define, ie \n. However, you could try something like $str = str_replace("\n", '', $str); maybe... i'm sorry, i'm not sure what you mean Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 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]); Quote Link to comment Share on other sites More sharing options...
colbyg Posted March 22, 2009 Author Share Posted March 22, 2009 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(). Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 I don't know, there isn't supposed to be, but it is apparent that there is, so get rid of it Quote Link to comment Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 You would be better to use file to get each line into an array in the first place. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 22, 2009 Share Posted March 22, 2009 Plus if the file has come from Windows I believe "\r\n" would be needed and not "\n" alone. Quote Link to comment Share on other sites More sharing options...
colbyg Posted March 22, 2009 Author Share Posted March 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
colbyg Posted March 22, 2009 Author Share Posted March 22, 2009 Plus if the file has come from Windows I believe "\r\n" would be needed and not "\n" alone. yes! thank you. that was the problem Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 22, 2009 Share Posted March 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 That's interesting, I didn't know that. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 22, 2009 Share Posted March 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
colbyg Posted March 22, 2009 Author Share Posted March 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 22, 2009 Share Posted March 22, 2009 If your text editor supports it (I know Notepad++ does) search for "\n" and replace with "\r\n" and everything should go back to how it should be. Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 23, 2009 Share Posted March 23, 2009 Yeah, I've noticed that as well lol. Espeically with file uploads in PHP, and then downloading it again via FTP Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.