# Retaining spaces in text fields

**URL:** https://forum.kirupa.com/t/retaining-spaces-in-text-fields/183515
**Category:** programming
**Created:** [March 27, 2006, 9:20pm UTC](https://forum.kirupa.com/t/retaining-spaces-in-text-fields/183515 "2006-03-27T21:20:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![SlowRoasted](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@SlowRoasted](https://forum.kirupa.com/u/SlowRoasted)
#### Post date: [March 27, 2006, 9:20pm UTC](https://forum.kirupa.com/t/retaining-spaces-in-text-fields/183515/1 "2006-03-27T21:20:05Z")

</div>

How do I go about retaining the spacing in text fields? Like if someone hit enter twice to seperate paragraphs. Sorry for all the questions today guys:beam: 😊

---

<div class="post-metadata">

### Author: ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)
#### Post date: [March 27, 2006, 9:50pm UTC](https://forum.kirupa.com/t/retaining-spaces-in-text-fields/183515/2 "2006-03-27T21:50:59Z")

</div>

i can’t remember how i’ve done it in the past, but one option is to split the string into an array at the line break. then loop through array and delete any blank entries. then echo each out with a \<p\> before and \</p\> after.

---

<div class="post-metadata">

### Author: ![Ankou](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ankou/32/1351_2.png) [@Ankou](https://forum.kirupa.com/u/Ankou)
#### Post date: [March 27, 2006, 10:05pm UTC](https://forum.kirupa.com/t/retaining-spaces-in-text-fields/183515/3 "2006-03-27T22:05:23Z")

</div>

PHP? Use the nl2br() function.  
[http://us2.php.net/manual/en/function.nl2br.php](http://us2.php.net/manual/en/function.nl2br.php)

Or if you’re looking to break things up with different HTML tags (like the paragraph tag like bwh2 is talking about) use a regular expression and preg\_replace() – or another regExp replace function. You can then replace all line feeds with something like \</p\>\<p\>. You may need to add the start and end HTML tag to the start and end of the string as well.
