I have a text field in my form which asks the user to enter his/her info. I want that when i submit the form, the entered data has all line breaks replaced with a <br /> tag so that when this data is stored in the database and displayed later then the formatting is correct.
<form action="index.php" method="post" name="add" enctype="multipart/form-data">
<p id="bio" style="display:none;"><label class="left">Bio</label><textarea name="bio" rows="10" cols="50" class="contact"></textarea></p>
<p class="submit" style="text-align:center;"><input type="submit" name="add" value="Submit" /></p>
</form>
I know that i have to use str_replace, but I just don't know where to use it. I think I'm getting confused as to when the entered information will be formatted.
You should not alter data, when saving it (beside escaping of unwanted chars, and date-conversions to your desired format (UTC or else)). Instead alter it, when displaying it.
So, save it in the database with the \ns (allowing to easily edit it later on) and when displaying the data, simple use nl2br() around the variable, holding the text from the database.
Otherwise - upon edit - you would need to convert <br> back to \ns and again to <br>s upon save.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With