2

I have a textarea in which the user enters his data. I want to make the PHP code leave lines at places where the user has. Suppose the user enters -

My name is Harry. 
I live in LA.

The out put will be

My name is Harry.I live in LA.

Please tell me the function/command we use to solve this problem(I think its a PHP problem)

Thanks in advance.

HTML Code -

<textarea rows='1' cols='50' id='p-text' class='p-textarea' placeholder = 'Write something about yourself here..'> </textarea>

I will output this in a tag, not a textarea.

@zrvan-

Output on var_dump($_GET['t'); is

string(31) "My name is Harry. I live in LA."
user1025469
  • 95
  • 1
  • 7
  • 2
    how are you passing the data to PHP ? can you include your HTML ? – Manse Jan 04 '12 at 14:47
  • 2
    You are probably outputting the text on a HTML page. Use `nl2br()` to create HTML line breaks – Pekka Jan 04 '12 at 14:47
  • @ManseUK Im using AJAX to pass the data. – user1025469 Jan 04 '12 at 14:48
  • possible duplicate of [Capture newline from a textarea input](http://stackoverflow.com/questions/5844465/capture-newline-from-a-textarea-input) – hakre Jan 04 '12 at 14:49
  • Do you display the output in a browser? Because the textarea will have newlines and the browser will not display those as newlines unless you either wrap the output text in `
    ` or run something like `nl2br()` in PHP.
    – zrvan Jan 04 '12 at 14:51
  • @zrvan Im outputting this in a
    tag. I cannot use a
     tag
    – user1025469 Jan 04 '12 at 14:54
  • @user1025469: This seems to be a more or less common issue, check http://stackoverflow.com/questions/1183466/new-line-problem-when-doing-ajax-post-with-jquery for a solution (using a regular expression before sending the data and convert newline to `
    `)
    – zrvan Jan 04 '12 at 15:15
  • Yesterday I answered similar question refer that..if still some problem lemme noe..:) [Yesterdays Ques](http://stackoverflow.com/questions/8717701/php-form-mail-converting-line-breaks-to-spaces/8717759) – Rajat Singhal Jan 04 '12 at 14:54

2 Answers2

8

nl2br() should solve that if you're echo-ing the string. See: http://php.net/manual/en/function.nl2br.php

Make sure you don't strip the newlines when you add content to the database though.

mat
  • 1,619
  • 1
  • 15
  • 25
  • nl2br() is not working for me. Please suggest me something else – user1025469 Jan 04 '12 at 14:52
  • @user1025469 show your PHP ... what do you mean its "not working" – Manse Jan 04 '12 at 14:54
  • My php is simple. – user1025469 Jan 04 '12 at 14:56
  • 1. Do you strip the newlines when adding the string to the database? 2. Do you strip the newlines before you echo them? – mat Jan 04 '12 at 15:05
  • @user1025469: could you post the result of `var_dump ($_GET['t']);` (as an edit to your question), when you've sent in the contents of a textarea with line breaks? – zrvan Jan 04 '12 at 15:09
  • Im not putting this into the db yet due to this problem. No i dont strip the newlines. The code i commented is all thats in the code. – user1025469 Jan 04 '12 at 15:09
  • This is not the solution for this specific problem. Check http://stackoverflow.com/questions/1183466/new-line-problem-when-doing-ajax-post-with-jquery for more information. – zrvan Jan 04 '12 at 15:16
  • or: http://stackoverflow.com/questions/8728717/textarea-output-doesnt-leave-lines/8728745#8728745 – mat Jan 04 '12 at 15:21
3

In fact the output is something like this

My name is Harry.\nI live in LA.

just use http://php.net/manual/de/function.nl2br.php for that :)

/edit: mat was faster :) flag his answer as solution :)

Walialu
  • 4,096
  • 2
  • 27
  • 29
  • By a split second.. ;) That's the only thing i don't like about StackOverflow. I am sometimes typing half an hour on an answer to find out that someone posted the exact same a minute before me. Ah well, that's offtopic. – mat Jan 04 '12 at 14:59