<br /> tags appearing in textarea output

I have an input form with two textareas allowing a user to type in words separated by commas in each.

<form action="http://www.example.com/output.php" method="post">
<table border="0">
<tr>
<td>
<h3 class="title">Prefix</h3>
<textarea cols="34" rows="8" name="cat[]"></textarea></td>
<td>
<h3 class="title">Suffix</h3>
<textarea cols="34" rows="8" name="cat[]"></textarea></td>
</tr>
</table>

Enter words separated by a comma. 

<input type="submit" value="Go" /> </form> 

It then passes these to the output form which explodes the words from the commas and then concatenates them together until all possible permutations of the words are created. It then prints out the results into a textarea. My problem is that the output (whilst correctly formatted and have the linebreaks in between each permutation) has a br tag at the end of each line. Eg.

Read More
testtest2<br />
testtest2<br />
testtest4<br />
testetest2<br />
testetest2<br />
testetest4<br />

Output form:

$cat = $_POST['cat']; //trait set for textbox inputs
foreach(array_keys($cat) as $key){
$cat[$key] = explode(",", str_replace(' ','',$cat[$key]));
}    

function showCombinations($string, $traits, $i)
{
if ($i >= count($traits))
echo trim($string)."n";  
else
{
foreach ($traits[$i] as $trait)
showCombinations("$string$trait", $traits, $i + 1);
}
}
?>

<form name=form1 method=post action=''''>
<textarea><?php ShowCombinations('', $cat, 0); ?></textarea>
</form>
  1. When I remove the textarea tags for the output it works fine.
  2. When I leave the textarea tags and remove/replace echo trim($string).”n”; with “r” or ‘n’ or “nr”, the
    disappears but I also lose the linebreak
  3. Replace echo trim($string).”n”; with echo nl2br($string); then same result as 2.
  4. Replace with echo nl2br($string).”n”; then same result as 1.

Would appreciate any help. My noob brain is about to implode.

Leave a Reply

7 comments

  1. I’ll preface this by saying I use Blogger not WordPress but I imagine there are similarities. In Blogger there is a setting to convert line breaks into <br> tags. I guess it’s convenient if you’re not putting code snippets and the like in (if you’re blogging about cats or something) but not so much for programming. I had to turn it off and put in my paragraph tags, etc manually.

    Here is one less than ideal solution, which also mentions the issue of WordPress inserting <br> tags in forms.

  2. I think the <br /> tag is what you want in this case. Newline characters like n and r don’t do anything in HTML – all whitespace, including newlines, is ignored. The <br /> tag is essentially the HTML equivalent – it stands for “break”. I don’t know too much about PHP specifically, so I don’t know why it’s putting the <br /> there for you automatically, but that’s the correct HTML for what you’re describing.

    Technically, there are better ways to format your output then using those tags, but that’s more of an advanced topic. If you are a bit new to this, like you said, then just get it working this way for now, and then maybe sometime down the road you can learn about proper semantic HTML markup, CSS styling, etc.

  3. Try using this, I already test it…

    <!DOCTYPE HTML>
    <html>
    <head></head>
    <body>
    <?php 
    if(isset($_POST['status'])){
    $newLineCode = "<br/>";
    $message = $_POST['status'] ;
    $modifiedTextAreaText = ereg_replace( "n", $newLineCode, $message);
    echo " <p>Result of Code Snippet:</p> " . $modifiedTextAreaText ;
    }
    ?>
    <form action="" method="post">
    <textarea name="status"></textarea>
    <input type="submit"/>
    </form>
    </body>
    </html>
    
  4. I believe n should work. Perhaps you do not have or have an incorrect doctype.

    Example: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    If all else, I like lock’s answer. You could us a <div> and you could even style it to look similar to a <textarea> (or even better!)

  5. It took some looking, but after some time I was able to pinpoint these two locations (lines 154 & 165 – WP 2.8) in the wp-includes/formatting.php file.

    154     $pee .= '<p>' . trim($tinkle, "n") . "</p>n"; //<-- before
    154     $pee .= trim($tinkle, "n") . "n"; //<-- after
    
    165     $pee = preg_replace('|(?<!<br />)s*n|', "<br />n", $pee); // optionally make line breaks <-- before
    165     $pee = preg_replace('|(?<!<br />)s*n|', "n", $pee); // optionally make line breaks <-- after
    

    This took care of the paragraph and and break tags in my textarea field.

  6. I solved my problem by using strip_tags function. when user would edit the past post, it was saving
    in the database and displaying on the blog. But strip_tags function took care of it.