floridaflatlander Posted August 11, 2012 Share Posted August 11, 2012 I've been using echo'<p>'.str_replace("\n", "</p>\n<p>", $descrip).'</p>'; To echo strings and I noticed the string looks likes this <p>String is here </p> So I changed it to echo '<p>'.str_replace("\r\n", "</p>\n<p>", $descrip).'</p>'; and the strings look like I want it to look, like this <p>String is here</p> I googled what is the difference between \r and \n and usually people start saying one is for windows one is for linex, one is old the other newer ... My questions are, when using php what do you enter at the end of a string when you press enter? And can I use "\r\n" just as will as "\n" and it not come back and bite me (put everything in one big paragraph)? Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/ Share on other sites More sharing options...
jazzman1 Posted August 11, 2012 Share Posted August 11, 2012 You spelled it wrong - linex != linux. If your project is deployed on linux server, you can use only "\n". Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368667 Share on other sites More sharing options...
floridaflatlander Posted August 11, 2012 Author Share Posted August 11, 2012 Thanks for the reply If your project is deployed on linux server, you can use only "\n". My project is deployed on a linex server So I changed it to echo '<p>'.str_replace("\r\n", "</p>\n<p>", $descrip).'</p>'; and the strings look like I want it to look, like this <p>String is here</p> .......... can I use "\r\n" just as will as "\n" and it not come back and bite me (put everything in one big paragraph)? Once again I want my string to look like this <p>String is here</p>. And this is how I get it ... echo '<p>'.str_replace("\r\n", "</p>\n<p>", $descrip).'</p>'; So can I use "\r\n" just as will as "\n" and it not come back and bite me (put everything in one big paragraph)? Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368675 Share on other sites More sharing options...
jazzman1 Posted August 11, 2012 Share Posted August 11, 2012 I think, you must use windex server is better than linex Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368682 Share on other sites More sharing options...
floridaflatlander Posted August 11, 2012 Author Share Posted August 11, 2012 I think, you must use windex server is better than linex lol, I'll leave it the way it is now "str_replace("\n", "</p>\n<p>", $descrip)" before I do that. Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368684 Share on other sites More sharing options...
MMDE Posted August 11, 2012 Share Posted August 11, 2012 I think, you must use windex server is better than linex lol, I'll leave it the way it is now "str_replace("\n", "</p>\n<p>", $descrip)" before I do that. You may also just check if it contains \r\n before you decide what to do. http://www.linex.com Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368685 Share on other sites More sharing options...
Pikachu2000 Posted August 11, 2012 Share Posted August 11, 2012 My questions are, when using php what do you enter at the end of a string when you press enter? The constant PHP_EOL holds the correct end-of-line character sequence for the operating system, but if all you're trying to do is output a new line to keep the html source organized, \n is just fine, AFAIK. Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368687 Share on other sites More sharing options...
MMDE Posted August 11, 2012 Share Posted August 11, 2012 My questions are, when using php what do you enter at the end of a string when you press enter? The constant PHP_EOL holds the correct end-of-line character sequence for the operating system, but if all you're trying to do is output a new line to keep the html source organized, \n is just fine, AFAIK. Looks to me like he is splitting up some user input from a text area. Replacing "\n" with "</p>\n<p>", which potentially could mean you maybe end up with "\r</p\n<p>", or at least so I think. Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368688 Share on other sites More sharing options...
floridaflatlander Posted August 11, 2012 Author Share Posted August 11, 2012 .... Replacing "\n" with "</p>\n<p>", which potentially could mean you maybe end up with "\r</p\n<p>", or at least so I think. Something like that may be whats happening because I'm getting this <p>String is here </p> instead of this <p>String is here</p> perhaps I'm just being anal Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368692 Share on other sites More sharing options...
Christian F. Posted August 12, 2012 Share Posted August 12, 2012 What's happening here is that you're getting input from multiple sources, where Windows is using \r\n style newlines and all others (MacOS and Linux amongst others) are using only \n. If you want to have a function that removes both variants, you can either use a regular expression or something to the effect of the following: <?php $replace = array ("\r\n", "\n"); echo '<p>'.str_replace ($replace, "</p>\n<p>", $descrip).'</p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368806 Share on other sites More sharing options...
DavidAM Posted August 13, 2012 Share Posted August 13, 2012 I googled what is the difference between \r and \n and usually people start saying one is for windows one is for linex, one is old the other newer ... History Lesson: The \r is a Carriage Return (CR) and the \n is a New-Line (or Line Feed (LF)). The use of two separate characters comes from the time when, believe it or not, there were no video monitors on computers. The computer terminal was basically a typewriter. And if you think about a typewriter, this next part makes sense. The CR (Carriage Return) sent the carriage that was holding the paper back to the beginning of the line. The LF (Line Feed) shifted the paper up to start printing on the next line. They needed two separate commands because the only way to get BOLD print and some special characters was to print on the same line a second time. So you could send a CR to go back to the beginning of the line and start sending characters again to type on the characters that were just typed. If there were no special characters or bold printing to do, the two commands were sent one after the other -- CR LF -- at the end of a line. MS kept the two-character "End-of-Line" sequence (CRLF) for text files. Unix uses a single character (LF). I think Unix used a single character to save bandwidth in a time when 1200 baud modems were the norm. There was a time (I believe) when another OS used only (CR) as an end-of-line character. My questions are, when using php what do you enter at the end of a string when you press enter? And can I use "\r\n" just as will as "\n" and it not come back and bite me (put everything in one big paragraph)? In general Windoze will send CRLF when the user types in a TEXTAREA (at least I.E. used to do that). The only real reason to keep the two-character sequence is if you will be writing to a text file and sending that file to someone who will open it on Windoze. Even then, most text editors will handle the single-character EOL (MS Notepad will not). So, to keep things simple in your application, you can replace the CRLF pair with LF. So, the code given by ChristianF could be enhanced to: $replace = array ("\r\n", "\r", "\n"); $descrip = str_replace($replace, "\n", $descrip); echo '<p>'.str_replace ("\n", "</p>\n<p>", $descrip).'</p>'; Notes: In this case, you must use the double-quotes -- the control characters will not be interpreted in single quotes. The order of the entries in the array is important. If the CRLF pair does not come first, each will be replaced separately. Also, str_replace is recursive. That is, the replace is done for each element of the array. This is why I added the extra line of code above. The code would be equivalent to: $replace = array ("\r\n", "\r", "\n"); foreach ($replace as $char) { $descrip = str_replace($char, "\n", $descrip); } As you can see, all CRLF will be replaced with LF. Then all CR will be replaced with LF. Then all LF (including the ones we just put in) will be replaced with LF. If we used a different sequence in the array, and were replacing with "</P>\n<P>" then we could end up with: # What if we used $replace = array ("\r", "\n", "\r\n"); Start: Something\r\nelse 1st Iter: Something</P>\n<P>\nelse 2nd Iter: Something</P></P>\n<P><P></P>\n<P>else 3rd Iter: Something</P></P>\n<P><P></P>\n<P>else Which is clearly not what we wanted. In truth, we could leave the LF out of the $replace array entirely, since we do that replacement separately. $replace = array ("\r\n", "\r"); $descrip = str_replace($replace, "\n", $descrip); echo '<p>'.str_replace ("\n", "</p>\n<p>", $descrip).'</p>'; Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368896 Share on other sites More sharing options...
Christian F. Posted August 13, 2012 Share Posted August 13, 2012 Or you could just combine those two operations into one: $search = array ("\r\n", "\n"); $replace = array ("\n", "</p>\n<p>"); echo '<p>'.str_replace ($search, $replace, $descrip)."</p>\n"; I don't bother with replacing "\r" alone, because no system1 use it on its own. 1Ended with Mac OS 9, which was the last OS that used \r alone for newlines. Quote Link to comment https://forums.phpfreaks.com/topic/266955-to-use-rn-or-n-in-str_replace-what-do-you-enter-when-you-press-enter/#findComment-1368998 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.