I have a PHP/MySQL app that when a user enters a description of there club that it reports back on a group page. When a user enters there information the line breaks are removed and all becomes one long string. Is there anyway that I can keep there line breaks? Also there is a problem with ' showing up correctly but im sure it is related to the first issue.

Hi....
There are two problems mainly...
Firstly..
When you use a textarea for text purpose, it does not take the enter (line break) automatically...
For this purpose, when you post the information, in the php code, when you use $_POST... just add a small function in there..

<?php
$desc_field = str_replace("\n","<br>",$_POST['desc_field']);
?>

In this case when you are taking the value of the textarea thing, then you need to explicitly replace the \n (which php takes as its line break) by the <BR> tag, which is the html line break..

But be careful.. when u use it, if u directly display this info, on a html page, then no issue, because our data already have a line break.. but when you edit this information in the textarea (usually edit information), then dont forget to again replace the <BR> tag to \n.

<textarea><?php echo str_replace("<br>","\n",$desc_field); ?></textarea>

Secondly, for the apostrophe issue, you need to be careful.. sometimes we use apostrophe in our text..
when you insert it into a query, then the query fails
for.eg. "insert into tablename values('Hi i'm xyz','ahc data')";

notice one single apostrophe in i'm.. this gives error...

So when you take the text information from the text fields, then better to use addslashes() function provided by php..
This function automatically appends all such characters (like single quotes, double quotes) with a \ character (it is called escape character)..

But when you display this information from database, then use its reverse function stripslashes() to automatically remove all the slashes that are put by addslashes() function..

Usage:

<?php

//suppose form is submitted and desc_field in our text field
$desc_field = addslashes($_POST['desc_field']);
//it automatically replaces all ' and " with \' and \"
?>

Similarly, when you want to display this information or edit it..

My information: <?php echo stripslashes($desc_field); ?>
//This desc_field is from database
<input type="text" name="desc_field" value="<?php echo stripslashes($desc_field); ?>" />

Hope this solves your query...

All of those str_replace 's are unnecessary. There is a built in PHP function.

$formatted_text = nl2br($text);

After reviewing the text entered I found that the user input text as a copy and paste from work and some special characters came with it and I used the code above to change the characters to the proper ones. and all is good with the world thanks!

This question has already been answered. Start a new discussion instead.