Debt Consolidation - PS2 Xbox Nintendo Dreamcast Games - Property in Bulgaria - Free Myspace Layouts - Find jobs

PDA

View Full Version : The best replace for me


onlyican.com
Jan 7th 2006, 5:09 pm
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>

cytech
Jan 7th 2006, 6:29 pm
You can do this:

$newstring=VALUE_FROM_DB;

$newstring=str_replace("\n","<BR>",$newstring);
$newstring=stripslashes($newstring);

onlyican.com
Jan 8th 2006, 4:58 am
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.

TMan
Jan 8th 2006, 7:21 am
Why use str_replace? Php has the function nl2br (http://nl2.php.net/nl2br)I believe, which is made solely for this purpose so probably best.

onlyican.com
Jan 8th 2006, 7:40 am
So, back to my question

What is "better coding"

str_replace or
nl2br

TMan
Jan 9th 2006, 2:13 am
I would recommend nl2br, it's far more obvious what your code does that way. Opinions may vary however.

cytech
Jan 9th 2006, 10:03 am
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 :)

Djohn
Jan 9th 2006, 3:53 pm
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".

cytech
Jan 10th 2006, 7:03 am
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. :D

TMan
Jan 10th 2006, 7:12 am
I believe better coding to come down to readability, but I guess it depends where your priorities are. :cool:

cytech
Jan 10th 2006, 8:34 am
Ahh, but isn't that why we have comments :D