Jump to content

To use \r\n or \n in str_replace() & what do you enter when you press enter?


floridaflatlander

Recommended Posts

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)?

Link to comment
Share on other sites

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)?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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>';

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.