简体   繁体   中英

PHP MySQL New Lines / Whitespaces from textarea

I've a problem about whitespaces and new lines at the beginning of a textarea. I send a json string with ajax to the php script. Then I decode the string into an php array (with json_decode). So I have a string in this array with two line breaks at the beginning. If a print the text from the array, the line breaks are there, but if I store the text into the mysql database, the line breaks are gone... Only at the beginning of the string, the line breaks gone... At the rest of the string, the line breaks are ok.

Thanks for your answers! rob

Probably(hopefully) you are doing some escaping / sanitizing before you store in the database. That probably trims your result, while in pure php the result is not trimmed.

Use PHP serialize() to make sure that you information stays the way it needs to be saved. Then add to database. When retrieving, call unserialize() to get all of you lovely information back.

In most browsers textareas' value parsed in a way, that the first character is ignored if it's a new-line.

<textarea>Value</textarea> <!-- value will be "Value" -->

<textarea>
Value
</textarea> <!-- value will be "Value\n" -->

<textarea>

Value

</textarea> <!-- value will be "\nValue\n\n" -->

Checked using javascript in Chrome / FF / Opera.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM