Jump to content

Allowing user to create paragraphs and line breaks


lauralee

Recommended Posts

I have added a function in a helpers file so that an administrator can edit text for input and UPDATE, but I can't get the paragraph code to work. Does the administrator type in literally \n\n or "\n\n" or something else in order to create a paragraph in the text? I've tried both, but they simply show up as text characters in the result....No new paragraph. Am I missing something, or does the user type in something other than \n\n or "\n\n"?<?phpfunction html($text) { return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); } function htmlout($text) { echo html($text); }function bbcode2html ($text){// Convert Windows (\r\n) to Unix (\n) $text = str_replace("\r\n", "\n", $text); // Convert Macintoch (\r) to Unix (\n) $text = str_replace("\r", "\n", $text); //Paragraphs $text = '<p>' . str_replace("\n\n", '</p><p>', $text) . '</p>'; //Line breaks $text = str_replace("\n", '<br />', $text); return $text;}

Link to comment
Share on other sites

The \n escape sequence represents a newline (i.e. the character produced when you press the enter key), so that is what you would type. The display string "\n" would be escaped to "\\n".

Link to comment
Share on other sites

When I typed \n in the form where I wanted a paragraph to be placed and submitted the entry, it UPDATED the text with a literal \n where it was typed.

Link to comment
Share on other sites

if i am not missunderstanding...you need to use nl2br() for showing a paragraph(<br/>) in the place of new line.

Link to comment
Share on other sites

maybe you are looking for this? http://se2.php.net/manual/en/function.nl2br.php
I'm not sure how to use what is in that part of the manual, or how it applies to my problem. I just now retried to use the \n\n in the text field and it worked this time. Don't know what the difference was, but it works now.
Link to comment
Share on other sites

I'm not sure how to use what is in that part of the manual, or how it applies to my problem. I just now retried to use the \n\n in the text field and it worked this time. Don't know what the difference was, but it works now.
I found that the "\n\n" should have been '\n\n' in the code below. It was the use of the double quotes that kept the line break from working properly.//Paragraphs$text = '<p>' . str_replace("\n\n", '</p><p>', $text) . '</p>';//Line breaks$text = str_replace("\n", '<br />', $text);
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...