PHP - removing CR in html textarea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP - removing CR in html textarea

Post 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>
Attachments
message area with CR removed.png
message with CR.png
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: PHP - removing CR in html textarea

Post by internet-solution »

in str_replace, you are replacing <br> variations with \n. \n will be shown as line breaks in textarea
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP - removing CR in html textarea

Post 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?
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: PHP - removing CR in html textarea

Post by internet-solution »

did you read my previous post?
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP - removing CR in html textarea

Post 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.
Post Reply