hey What i got is a form, using <textarea> The form then goes to a MySQL db, as a text field Then that field is echoed to the website somewhere else. BUT as you may know using a form You Type we're going to the pub tonight You want to come. It will come out we/'re going to the pub tonight\nYou want to come but for the text echoed from the db, it needs to be <br> not \n WHats the best way to stripshlashes AND replace \n with <br>
You can do this: $newstring=VALUE_FROM_DB; $newstring=str_replace("\n","<BR>",$newstring); $newstring=stripslashes($newstring);
str_replace is the best, I aint done replace yet, so I didn;t know what was the best As it goes, I need slashes for the db anyway, and it is automaticly stripped on exit.
Why use str_replace? Php has the function nl2br I believe, which is made solely for this purpose so probably best.
I would recommend nl2br, it's far more obvious what your code does that way. Opinions may vary however.
I think it depends on how large the string is. If the string is quite large it is probably best to use preg_replace. I have never used nl2br but Reading up on it - thats what its made to do. So I say go with it
I always use nl2br and I fail to see how using alternate solutions as opposed to an existing function - for that very purpose - would be "better coding".
well, "better coding" comes down to speed correct? So, preg_replace has proven itself to be the fastest search replace with large files and strings. BUT if you are working with small strings nl2br is great.
I believe better coding to come down to readability, but I guess it depends where your priorities are.