Jump to content

Archived

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

pndof12006

Automatic <br>

Recommended Posts

Ok, I am starting a poetry website and when users submit a new poem they currently need to add a [code]<br>[/code] at the end of each line. is it possible to make it so when the form is submitted it automatically puts the breaks in the databse?

Share this post


Link to post
Share on other sites
something like this maybe:

[code]$var = $_POST[poem]."<br/>";[/code]

then put  $var into the database insert query

Share this post


Link to post
Share on other sites
.. a newline character, you mean?  because that's EXACTLY what hitting enter in a text field does.  why don't you actually try using it before claiming it won't work?

Share this post


Link to post
Share on other sites
you won't see them because they get converted into the actual linebreaks you see.

[code]echo nl2br($value_pulled_from_db);[/code]

try that.

Share this post


Link to post
Share on other sites
when it gets inserted in the DB it is in the same format it ws in the text area. example

SAMPLE
SAMPLE

will show like that in the Text Area and in The DB, But when echoed to a page it is like this:

SAMPLE SAMPLE

Share this post


Link to post
Share on other sites
then let's see:

a) the MySQL select query that grabs the value from the database
b) the PHP code that echoes nl2br() of that value

Share this post


Link to post
Share on other sites
pndof12006: Let me explain how it works:

\n (LF - line feed) - Linux uses this to display a new line
\r (CR - carriage return) - Macintosh use this to display a new line
\n\r (CRLF - carriage return, line feed) - Windows uses this to display a new line

[url=http://php.net]nl2br[/url] converts LF to [tt][nobbc]<br />[/nobbc][/tt]. If you want to convert all types of new lines to \n, then use this: [code]$string = preg_replace("/(\r\n|\r|\n)/","\n",$string);
$string = nl2br($string);[/code]

Or perhaps even better: [code]$string = preg_replace("/(\r\n|\r|\n)/","<br />\n",$string);[/code]

Share this post


Link to post
Share on other sites
[code]$p = preg_replace("/(\r\n|\r|\n)/","\n",$poem);
echo "<tr><td align='center'><b>$pname</b></td></tr>";
echo "<tr><td>&nbsp;</td></tr>";
echo "<tr><td align='center'>";
echo $p;
echo "</td></tr>";
echo "<tr><td>&nbsp;</td></tr>";[/code]

Share this post


Link to post
Share on other sites
you forgot the additional nl2br() if you're going to go with his first suggestion.

[code]echo nl2br($p);[/code]

Share this post


Link to post
Share on other sites
try this:

[code]echo "<tr><td align='center'><b>$pname</b></td></tr>";
echo "<tr><td>&nbsp;</td></tr>";
echo "<tr><td align='center'>";
echo nl2br($poem);
echo "</td></tr>";
echo "<tr><td>&nbsp;</td></tr>";[/code]

Share this post


Link to post
Share on other sites
alright, give these a shot then:

[code]$p = preg_replace("/(\r\n|\r|\n)/","<br />\n",$poem);
echo $p;[/code]

[code]$p = preg_replace("/(\\r\\n|\\r|\\n)/","<br />\n",$poem);
echo $p;[/code]

Share this post


Link to post
Share on other sites
You'll probably find that his code strips \n before inserting it into the database or that his database stores line breaks as \r only.

Share this post


Link to post
Share on other sites

×

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.