GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Feb-2009, 12:56
wendyz wendyz is offline
New Member
 
Join Date: Feb 2009
Posts: 6
wendyz is on a distinguished road

Why "\n" is ignored when store into database


I create form in PHP, and when I type something in text area will be stored into database. But when it display on screen, "\n" will be ignored. I check the table in database, the several lines becomes one line. I set charset is utf-8, suppose that it will get original format with content from text area. anyone knows how to solve this problem?

example: I typed in text area:
"hi anyone,
I have question about PHP,
Could anyone help to solve the problem?
thanks!"

the output is:
"hi anyone,I have question about PHP,Could anyone help to solve theproblem?thanks!"

I want to get original format. How to do?
  #2  
Old 23-Feb-2009, 14:00
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Why "/n" is ignored when store into database


Are you viewing the output in an HTML page? If so, you need to insert <br> tags. You can do that with preg_replace. Example:

PHP Code:

$foo = preg_replace("/\n/", "<br>\n", $foo); 


__________________
www.blake-foster.com
  #3  
Old 23-Feb-2009, 14:39
wendyz wendyz is offline
New Member
 
Join Date: Feb 2009
Posts: 6
wendyz is on a distinguished road

Re: Why "/n" is ignored when store into database


Thanks! It works now.
  #4  
Old 23-Feb-2009, 14:39
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Why "/n" is ignored when store into database


You're welcome.
__________________
www.blake-foster.com
  #5  
Old 23-Feb-2009, 23:17
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 858
admin will become famous soon enough

Re: Why "/n" is ignored when store into database


Also, look at the PHP function nl2br(). Details: http://www.php.net/manual/en/function.nl2br.php.

As to your original question, the newlines are saved to your database and not "ignored" at all. If you look at your web page source (CTRL+U), you will see that the newlines are still there in your markup. However a web browser handles these newlines and carriage returns differently based on the HTML tags the string is in. If you want to preserve/display the text exactly the way you want, you need to place them inside <pre></pre> tags and if not, you should use the above-mentioned nl2br function.

For example (within PRE tags):

PHP Code:

<?php

// $row[1] holds the string/text off the database.

echo "<pre>", htmlspecialchars( $row[1] ), "</pre>";

?>

output returned to the web browser:
HTML Code:
<pre>hi anyone, I have question about PHP, Could anyone help to solve the problem? thanks!</pre>

Example, with PHP's nl2br function:

PHP Code:

<?php

// $row[1] holds the string/text off the database.

echo "<p>", nl2br( htmlspecialchars($row[1]) ), "</p>";

?>

resulting in this markup being returned to the web browser:
HTML Code:
<p>hi anyone,<br /> I have question about PHP,<br /> Could anyone help to solve the problem?<br /> thanks!</p>
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Restore Database ajitham .NET Forum 2 16-Feb-2006 05:09
store values skyloon MySQL / PHP Forum 6 23-Jul-2005 03:29
Limit combo box and date time picker choice according to database created in folder shinyhui C++ Forum 0 22-Feb-2005 20:16
Limit combo box and date time picker choice according to database created in folder shinyhui MS Visual C++ / MFC Forum 0 22-Feb-2005 02:13
Limit combo box choice according to database created in folder shinyhui MS Visual C++ / MFC Forum 3 21-Feb-2005 21:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:01.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.