Jump to content


Photo

Automatic <br>


  • Please log in to reply
21 replies to this topic

#1 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 06:59 AM

Ok, I am starting a poetry website and when users submit a new poem they currently need to add a
<br>
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?

#2 trillion

trillion
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 16 October 2006 - 07:16 AM

something like this maybe:

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

then put  $var into the database insert query

#3 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:17 AM

but that will not put the
<br>
at the end of each line of a poem.

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 October 2006 - 07:18 AM

look up nl2br() in the php manual.

#5 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:27 AM

that doesn't help me, from what it looks like u have to have a \n in order for that to work.

#6 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 October 2006 - 07:29 AM

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

#7 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:30 AM

well the thing is, when it's submitted and inserted into the DB there is no characters that are /n.

#8 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 October 2006 - 07:35 AM

you won't see them because they get converted into the actual linebreaks you see.

echo nl2br($value_pulled_from_db);

try that.

#9 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:37 AM

didn't work.

#10 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:39 AM

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

#11 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 October 2006 - 07:41 AM

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

#12 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 07:42 AM

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

nl2br converts LF to <br />. If you want to convert all types of new lines to \n, then use this:
$string = preg_replace("/(\r\n|\r|\n)/","\n",$string);
$string = nl2br($string);

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


#13 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:44 AM

Daniel it didn't work.

#14 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 07:45 AM

Try to post the script that posts the poem.

#15 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:47 AM

$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>";


#16 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 October 2006 - 07:49 AM

you forgot the additional nl2br() if you're going to go with his first suggestion.

echo nl2br($p);


#17 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:50 AM

still doesnt do nething

#18 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 October 2006 - 07:54 AM

try this:

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


#19 pndof12006

pndof12006
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 October 2006 - 07:55 AM

doesn't do nething, still shows SAMPLE SAMPLE

#20 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 October 2006 - 08:00 AM

alright, give these a shot then:

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

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users