Forum Moderators: coopster

Message Too Old, No Replies

textarea, newlines and str replace

doesnt work.

         

d40sithui

3:38 pm on Sep 20, 2007 (gmt 0)

10+ Year Member



Hi all,
I ran into a really annoying problem that I can't solve! Maybe someone can lend a hand. I have a form with a <textarea> field.
On the action script, i try to use str_replace to get rid of the "\r\n" in the textarea, but it doesnt work. I created a static variable with an few "\r\n"s and was able to use str_replace with that one.

ok so this does NOT work:
$new_course_poc = clean_var(mysql_less_safe($_POST['new_course_poc']));
$order = array("\r\n", "\n", "\r");
$replace = '<br/>';
echo $new_course_poc."<br>";
$new_course_poc = str_replace($order, $replace, $new_course_poc);
echo $new_course_poc."<br>";

--------------
but this does--
$test = "hello\r\nyou\r\nthere\r\n";
echo $test;
$test = str_replace($order, $replace, $test);
echo $test;

any ideas?

eelixduppy

3:40 pm on Sep 20, 2007 (gmt 0)



There's a nifty function, nl2br [us2.php.net] that may interest you :)

d40sithui

3:54 pm on Sep 20, 2007 (gmt 0)

10+ Year Member



thanks eelix. i have to switch things around a bit, but this works ok.
but i cant help but wonder why str_replace ddidnt work for it.

whoisgregg

4:41 pm on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either your clean_var or mysql_less_safe functions are probably escaping the \r and \n characters using addslashes or mysql_real_escape_string. If that's the case, you'd need to do a str_replace for "\\\r\\\n" (or something like that I've never really learned how to count up all the slashes -- echo out the $new_course_poc to see what they are being turned into).

d40sithui

11:59 am on Sep 21, 2007 (gmt 0)

10+ Year Member



haha i figured it was something stupid i did. thanks