Results 1 to 6 of 6

Thread: line breaks from textarea json_encode

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default line breaks from textarea json_encode

    i have a textarea input field and when you hit the return key json_encode will output the actual break, not interpret it as \n or <br> like this. this line breaks my code
    Code:
    {"data":[
    	{"index":3,"text":"blue
    	","img":"blue.jpg"},
    	{"index":2,"text":"green","img":"green.jpg"},
    	{"index":1,"text":"something","img":"yellow.jpg"}
    ]}
    i would like it to output like this
    {"data":[
    {"index":3,"text":"blue\n","img":"blue.jpg"},
    {"index":2,"text":"green","img":"green.jpg"},
    {"index":1,"text":"something","img":"yellow.jpg"}
    ]}
    heres a truncated section of my code
    Code:
    <form id="itmSubmit" method="POST">
    	<textarea id="entryItm" name="entryItm" cols="50" rows="1" wrap="off"></textarea><br/>
    	<input class="itmSubmit" type="submit" value="enter" />
    </form>
    
    
    $nTitle = $_POST['entryItm']; // get posted comment
    $dataArr = array('index' => $num++, 'text' => $nTitle, 'img' => 'blank.jpg'); 
    array_unshift($dataList['data'], $dataArr);
    $dataTxt = json_encode($dataList);
    is there a way to output the enter key hit as \n in the json output?
    Last edited by ggalan; 09-15-2011 at 05:28 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You could assign the value of the field using preg_replace() to an interim variable, and then use that variable in place of its value or it's value's place in the array.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ggalan (09-15-2011)

  4. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    tried this
    Code:
    $nTitle = preg_replace("\n", "<br/>", $nTitle);
    but now i get a message
    Code:
    Warning: preg_replace() [function.preg-replace]: Empty regular expression

  5. #4
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    got it
    Code:
    $nTitle = trim( preg_replace( '/\s+/', '<br/>', $nTitle ) );

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Using \s is any whitespace between words including tabs and spaces. The + after it would eliminate all but two or more of those in a row, but not all of those occurrences are line breaks, and not all line breaks qualify for that, and you said you wanted \n not <br/> though that might be preferable in this case if workable.

    Instead of \s, you might want \n, but the trouble with that is that the kind of line break depends upon the OS of the browser and upon the browser. It can be \n or \r or \r\n

    Perhaps:

    Code:
    $nTitle = trim( preg_replace( '/(\r\n)|\n|\r/', '<br/>', $nTitle ) );
    or for replacement with \n:

    Code:
    $nTitle = trim( preg_replace( '/(\r\n)|\n|\r/', '\\n', $nTitle ) );
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    regex always gets confusing : (
    Last edited by ggalan; 09-15-2011 at 07:46 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •