Page 1 of 1

PHP - removing CR in html textarea

Posted: Wed Jan 04, 2012 10:57 am
by cjkeane
Hi everyone,

I have an issue with a text area in an html form. When data is retrieved from a mysql database, the message area displays 'br '. I can use str_replace, which could eliminate the 'br ', but the message area still assumes there is a CR.

Does anyone have any ideas on how to resolve this issue?

This is the coding I'm using to remove any variation of 'br', however there are still line breaks on every line and there shouldn't be.

Code: Select all

<?php 
     $originalmsg = $row['body'];
     $contentfixed = str_replace('</p>', '</p><br />', "$originalmsg");
     $contentfixed = strip_tags($contentfixed, '<br>');
     $contentfixed = str_replace("<br />&nbsp;<br />", "\n", "$contentfixed"); 
     $contentfixed = str_replace("<br />", "\n", "$contentfixed"); 
     $contentfixed = str_replace("<br>", "\n", "$contentfixed"); 
     $contentfixed = str_replace("<br ><br >", "\n", "$contentfixed"); 
     $contentfixed = str_replace("<br ><br ><br >", "\n", "$contentfixed");
     $contentfixed = str_replace("&nbsp;<br />", "\n", "$contentfixed"); 
?>
<textarea name="ActionTextField" cols="60" rows="30" id="ActionTextField" ><?php echo $contentfixed; ?></textarea>

Re: PHP - removing CR in html textarea

Posted: Wed Jan 04, 2012 12:49 pm
by internet-solution
in str_replace, you are replacing <br> variations with \n. \n will be shown as line breaks in textarea

Re: PHP - removing CR in html textarea

Posted: Wed Jan 04, 2012 2:13 pm
by cjkeane
i've changed it to the following:

Code: Select all

<?php
    function br2nl($string) {
    $string = str_replace("\r\n", "\n", $string); // make from windows-returns, *nix-returns
    $string = str_replace("<br />", "\n", $string); // to retrieve it
    $string = str_replace("<br/>", "\n", $string); // to retrieve it
    return $string;
}
 $originalmsg = $row['body'];
$contentfixed = br2nl($originalmsg);
?>
if i do this:

<pre> <?php echo $contentfixed; ?></pre> // the content is is displayed correctly

however if i do this:

<pre> <textarea name="ActionTextField" cols="60" rows="30" readonly="readonly" id="ActionTextField" ><?php echo $contentfixed; ?></textarea></pre>

the content isn't displayed correctly. Any ideas why?

Re: PHP - removing CR in html textarea

Posted: Thu Jan 05, 2012 3:26 pm
by internet-solution
did you read my previous post?

Re: PHP - removing CR in html textarea

Posted: Thu Jan 05, 2012 3:56 pm
by cjkeane
yes i did and i understand your point. if i replace br with "" then the whole text area becomes one long wrapped line. it seems that i still need to maintain the \n.

i have tried to eliminate multiple br's, but that too causes a problem but i think i know how to resolve it . thanks.