ErcFrtz Posted November 5, 2009 Share Posted November 5, 2009 Hello, I have the following code: if(file_exists($pigIDFile) && is_readable($pigIDFile)) { $readFile = file($pigIDFile); foreach($readFile as $line) { $pigIDarray[] = $line; } } My question is with this code it puts the new line character at the end of each entry and I just want each line without that new line character. How do I eliminate it from $line so that my array is populated with each line without the new line character? Quote Link to comment https://forums.phpfreaks.com/topic/180458-solved-eliminating-the-new-line/ Share on other sites More sharing options...
mikesta707 Posted November 5, 2009 Share Posted November 5, 2009 $string = str_replace('\n', '', $string); Quote Link to comment https://forums.phpfreaks.com/topic/180458-solved-eliminating-the-new-line/#findComment-951987 Share on other sites More sharing options...
ErcFrtz Posted November 5, 2009 Author Share Posted November 5, 2009 This method is not working. I've tried exploding the string and eliminating it that way too but it's a single word on each line and it doesn't seem to eliminate the problem at all. Quote Link to comment https://forums.phpfreaks.com/topic/180458-solved-eliminating-the-new-line/#findComment-951994 Share on other sites More sharing options...
.josh Posted November 5, 2009 Share Posted November 5, 2009 str_replace needs to have the \n wrapped in double quotes not single. Alternatively, you can use trim Quote Link to comment https://forums.phpfreaks.com/topic/180458-solved-eliminating-the-new-line/#findComment-951996 Share on other sites More sharing options...
physaux Posted November 5, 2009 Share Posted November 5, 2009 try printing your string with "echo htmlspecialchars($string);" That should show you the raw string, maybe you can find some different line break thingy. have you tried replacing "\r"? That is apparently another new line char Quote Link to comment https://forums.phpfreaks.com/topic/180458-solved-eliminating-the-new-line/#findComment-951997 Share on other sites More sharing options...
mikesta707 Posted November 5, 2009 Share Posted November 5, 2009 oh sorry, yeah $string = str_replace("\n", '', $string); forgot about that Quote Link to comment https://forums.phpfreaks.com/topic/180458-solved-eliminating-the-new-line/#findComment-952000 Share on other sites More sharing options...
ErcFrtz Posted November 5, 2009 Author Share Posted November 5, 2009 Thank you. using trim() worked. Quote Link to comment https://forums.phpfreaks.com/topic/180458-solved-eliminating-the-new-line/#findComment-952006 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.