▼  Site Navigation Main Articles News Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc Links to Webcomics Bible Quotes About Older Musings
site version 7.3
Carriage Returns
written by: admin


Date Written: 2/26/07 Last Updated: 6/19/13

This page demonstrates to some degree a script that will convert carriage returns into actual carriage returns that operate the same way (with a few exceptions) as the ones you enter into a wordperfect text file for a school paper and such.

A carriage return is what happens to your cursor when you hit the return or enter key on your keyboard.  It is also the term used for those old style typewriters when a person reaches the end of a line there was a manual linefeed lever so that a new line could be typed upon.  PHP recognizes the keystroke of a carriage return as an invisible code of \r\n also called a line terminator.  \r is known as a carriage return and \n is known as a newline.  Unfortuantely this bit of code is not expressed as is with PHP, but is, for the most part, rendered inert.  What we want to do is give those carriage returns their original power back to at least separate paragraphs again.

Notice the blank line that exists between paragraphs?  In oder to get that effect you must type <p> at the beginning of each new paragraph or else enter <br><br>.  This standard procedure can be replaced with the more natural carriage returns that is otherwise known as simply hitting the enter button twice before typing the next paragraph.  How?

This program that is being used to type out this page recognizes carriage returns, and converts them into linebreaks: <br>.

Here is the code that I added to my script.

$summary=htmlentities($summary);    
$summary=preg_replace("/\r\n/","<br>",$summary);
$summary=html_entity_decode($summary);
$summary=preg_replace("/&lt;p>/","",$summary);

echo "$summary";


The first line converts the string into htmlentities so that the carriage returns are recognized by PHP as \r\n while other symbols like < appear as &lt; even though the carriage returns still are not displayed.  I don't know why PHP does not display the carriage returns with any of their built in functions and there are about 6 to 12 different built in functions available for displaying information in various ways.  With this line the PHP will at least be able to recognize the carriage returns.

The second line replaces all instances where I hit the enter key on my keyboard and converts it into a line break.

The third line converts the string back so as to look like it did when I was first typing the document complete with font colors for the orange bits shown above only now the string contains line breaks instead of carriage returns which were not recognized.  

The fourth line now removes or ignores all instances of <p> so that my previous documents will not have to have the <p> tags deleated from all of my old documents, but I can if I want.  The reason I can ignore them is that all of my previous documents happened to already have the carriage returns entered in before every paragraph tag for editing purposes, which before was the only way that they could be seen at all.  Now the carriage returns are not edited out of every document and actually have a meaning.  Either way the effect on the displayed document will be the same now, because the <p> will now be ignored while the carriage returns will be recognized.  If you want the paragraph breaks to be interpreted in the normal way just add a space, like this <p > as opposed to <p> and it will not be ignored.

Note: Multiple spaces typed in a row are also rewritten by HTML and PHP and such so that only one space is displayed for every two or more listed together.  What if I want to display extra spaces in a document?  Use $summary=str_replace('  ','&nbsp;&nbsp;'$summary);.  See what I did here.

Note: carriage returns will not normally be displayed in a document at all.

Note: leading newlines are ignored if it is displayed immediately after a start tag or before an ending tag. ref

Here is my initial workaround:
$rr=substr($summary,0,2);
$rr=htmlentities($rr);    
$rr=preg_replace("/\r\n/"," \r\n",$rr);
$rr=html_entity_decode($rr);
$summary=substr_replace($summary,$rr,0,2);$rr='';


Honestly, I have only seen this happen with leading newlines in textareas.  I cannot replicate this with newlines following <p> tags or with trailing newlines, so the code above works just fine for me.

TAGS: php, browser
copyright 2005–2024