mysql and new lines
Suppose, this is my variable
$var = "
sadkfljsl;dfjsa skdf kls n ksdjfn smdf sd
lfsdhf d ,.scdn lkbn lkdsaf slkjdfhsd fnsildfu dfskdfnsndfjygsdf ";
when i try to insert $var into the dbase.. it looses the \n's.... any reason why? any way around it?
Take it out of the database and print it onto the page, then view the source code and you'll see there are line breaks
To turn into real line breaks use:
$var = str_replace("\n", "<br>", $var);
naa.. there are no \n or <br> or anything like that in the dbase itself.. its like they dun get stored :(
use addslashes() for all your data?
You shouldn't be able to see the \n in the databases, they do get stored though.
addslashes wont work.. coz all it does is, add \ before every " and other special characters... nothing esle...
as for not being able to the \n's... well.. ok... but then but why arnt the right things showing up? why arnt the lines showing up?
i tried the code you gave me.. it dint work :(
hmmmmm......ok.. the code worked... but isnt there an easier or faster way? coz, there are waaaaay to many files to edit this way :(
$var = nl2br($var);
is about the only other way.
The draw back of nl2br is that it produces <br>, which if you are creating XHTML compliant pages are not valid. That is why str_replace is better as you can replace the new lines with the compliant <br />.
On php.net however, in one place they say it replaces it with <br />, and in other places it says <br>.
You are losing the visible newlines because HTML ignores newlines - in HTML you have to have <br> or <br /> tags instead. The newlines are stored in the browser, but when stuff is outputted to HTML they are no longer visible unless you replace them with <br> tags.
This is a GOOD thing. There are many applications that do NOT require <br> tags to generate new lines - such as e-mail sending. If the data was stored in the database with <br> tags instead of newlines it would make it much harder to use the data in situations other then HTML, such as formatting data for hand held PCs or e-mailing articles to people.