Displaying Formated Text

Hi folks,

When user submit a property in our propery portal, the property description has a text area.
user may format the description with line breaks etc. but when displaying the property details when viewing the property, evything is printed in a continues line.

a google search brought me an idea that tells, when saving the property description to mysql, it askes to wrap the description with pre tags.
so i did the same. but when i dispaly the preopty inside a table cell, the description is printed in one single line that even by passes the table boundry.
but if i remove the pre tags when displaying the property, then it correctly fits the table cell width. but no formatting.

whats the solution for this?

Does your script replace user entered “Enter” (new lines: \n) with
(to force line break)? Doesn’t sound like you did.

1 Like
//description//
$description=mysql_real_escape_string($_GET['description']);
$description="<pre>" . $description . "</pre>";
}

when displaying

$description=$row['description'];

i also tried another method for saving without using pre method

$description=mysql_real_escape_string(nl2br($_GET['description']));

but still description is showing without newline.

Simply save the data in as is (That is if you’re using prepared statements) then just do something like:

  echo nl2br($comment) . "\n";

php.net nl2br

1 Like
echo "<td colspan='4'>" . nl2br($row_detail['description']) . "\n" . "</td>";

problem remain same :frowning:

sample output

  1. abc2. abc3. abcd

but it must be

  1. abc
  2. abc
  3. abc

Perhaps you could use a regular expression to apply the desired formatting.

Might need go something like this if there are extra slashes saved to DB.

str_replace(array("\\r\\n","\\n","\\r"),array("<br />","<br />","<br />"),$row_detail['description']);

You could include or use single slash replacement only if needed. All depends on how data is being stored.

str_replace(array("\\r\\n","\\n","\\r","\r\n","\n","\r"),array("<br />","<br />","<br />","<br />","<br />","<br />"),$row_detail['description']);

My problem becoming more complicated.

first i want to know, when i save the text area data having line brakes to mysql, mysql will save the data continually or exactly as in the text area or with \n s??

ex:

  1. Test
  2. Test

mysql will store this as

  1. test2. test [this is what i have in db when view thorugh phpmyadmin]

or

1.test
2.test

or

1.test\n2.test

which way above?

*assume when saving data no function is used to format data such as stripslashes , mysql_real_escape etc

Data is being saved with carriage return and line break \r\n. You don’t SEE the literal \r\n as they are being rendered to the screen. If you are using mysql_real_escape_string() you will have \\r\\n saved to the DB.

1 Like

Perhaps this code might help explain what is going on. It’s standalone so just put it in a file in your web directory.

<h1>New line Formatting</h1>

<form method="POST">
    <textarea rows="4" cols="20" name="data"> </textarea>
    <input type="submit" />
</form>
<?php
$postedData = isset($_POST['data']) ? $_POST['data'] : "Line 1\nLine 2\nLine 3";

$escapedData = mysql_real_escape_string($postedData);

$restoredData = str_replace(array('\n','\r'),array("\n",''),$escapedData);

?>
<p><?php echo $postedData; ?></p>
<p><?php echo $escapedData; ?></p>
<p><?php echo nl2br($restoredData); ?></p>

mysql_real_escape_string changes the actual new line control characters into literals. After retrieving that values from your database you need to convert the literals back into control codes. You then use nl2br to change the new lines into br’s so everything displays properly.

Don’t use nl2br if you are echoing back into the content portion of the textarea element.

You really should try to avoid mysql_real_escape_string. mysql handles control codes just fine. Prepared statements will allow you to avoid all this conversion nonsense. But you can make it work once you understand what is happening.

1 Like

Hai ahundiak,
here is the result of the above in our server.

You wouldn’t have this problem if you designed the database properly. Each line should be a separate record in the table. If they are supposed to be numbered then you’d then display them as an ordered list with no need for extra formatting at all.

Hai felgall,
nop, we cant expect evey one will list their property detail in numbered fashion.

whats happening in this line really ? i did not undestand this shorthand method.

$postedData = isset($_POST['data']) ? $_POST['data'] : "Line 1\nLine 2\nLine 3";
$postedData = isset($_POST['data']) ? $_POST['data'] : "Line 1\nLine 2\nLine 3";

It’s called a ternary statement. http://php.net/manual/en/language.operators.comparison.php Search for ternary once you get to the page.

If you type something in your textarea and press submit then $postedData will be set to whatever you type. Otherwise it gets set to the default string of “Line 1\nLine 2\nLine 3”. Just a way of testing.

Not to be overly snarky (some would say arrogant) but it does require a certain amount of training to write php. If what I posted really stumps you then you might need to explore some basic tutorials. Even if you get past this fairly straight forward problem then you will just run into something else without learning the basics.

1 Like

In this particular case it is also called a security hole since it is assigning a tainted $_POST value to another field without first validating or sanitizing it.

1 Like

As much as I have no problem with “asides” that point out things that should be considered, I feel they should be noted in passing and the focus of discussion should remain on the original topic or at least where the OP has progressed it to.

When members deviate from the discussion for too long it is a form of
“thread hijacking” and not fair to the OP or anyone else reading the topic looking for related information.

Please keep the discussion On-Topic.

If any feel strongly enough about something, please start your own new topic.

If any have something to say to a particular member, please do it via Private Message.

Forum topics are NOT the place for arguments

1 Like

strange, pls take below two scenario.

first one saves the data to mysql without the new line (screen shot below)
but second one does. phpmyadmin shows the data in multiple lines correctly (screen shot attached).
same coding, but one use ajax to post data while other just submit form.

add-property.php

<textarea name="description" id="description" cols="45" rows="5"></textarea>

reg-property.php

$description=$_GET['description'];

$query = "INSERT into properties VALUES  ('','$uid','$title','$type','$bedrooms','$bathrooms','$area','$district','$address','$city','$zip','$description','$contact','$phone','0','$price','$numdays','$dt','','','','','','')"; 
if($result=mysql_query($query) or die (mysql_error())); 

  • data is passed using ajax method
  • data type of the filed in db is text

form.php

<textarea name="data" cols="" rows=""></textarea>

save.php

$query = "INSERT INTO test VALUES('$data')"; 
if ($result=mysql_query($query) or die (mysql_error())); 

  • data is passed using post / submit form
  • data type of the filed in db is text

What’s going on folks? it took now almost 3 days to solve this text area data saving problem.

Hai folks,

my problem solved finally. Thanks for all your support. i had learned a lot in this thread.
actually Ajax is the culprit. it removes the new line when it passes the values to php script.
so i had to use the encodeURIComponent to preserver the new line.

var description=encodeURIComponent(document.getElementById("description").value);

when retriving the data i m just using nl2br and it works !!

Thank you every body !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.