Tags appearing in textarea output I have an input form with two text boxes that allows the user to enter words separated by commas in ea...">

<br/"> Tags appearing in textarea output

I have an input form with two text boxes that allows the user to enter 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 them into an output form that explodes the words from commas and then concatenates them together until all possible word permutations have been created. Then it outputs the results to a text box. My problem is that the output (while correctly formatted and has line breaks between each permutation) has a br tag at the end of each line. For instance.

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

      

Withdrawal 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>

      

  • When I remove the textarea tags for output, it works fine.
  • When I leave the textarea tags and remove / replace echo trim ($ string). "\ n"; with "\ r" or "n" or "\ n \ r"
    disappears, but I also lose linebreak
  • Replace echo correction ($ string). "\ n"; with echo nl2br ($ string); then the same result as 2.
  • Replace with echo nl2br ($ string). "\ n"; then the same result as 1.

Would be grateful for any help. My noob brain is about to explode.

+1


a source to share


7 replies


I'll cover this by saying that I'm using Blogger and not Wordpress, but I think there are similarities. Blogger has a setting to convert line breaks to tags <br>

. I think this is convenient if you don't add code snippets and the like. (If you blog about cats or whatever), but not so much for programming. I had to disable it and put it in my paragraph tag etc. Manually.



Here is one less than ideal solution that also mentions the issue of Wordpress inserting into tags <br>

in forms.

+2


a source


I think the tag <br />

is what you want in this case. Newlines like \ n and \ r do nothing in HTML - all spaces, including newlines, are ignored. The tag <br />

is essentially the HTML equivalent — it denotes a "break". I don't know too much about PHP, so I don't know why it automatically puts in <br />

for you, but this is the correct HTML for what you are describing.



Technically, there are better ways to format the output and then use those tags, but this is a more advanced topic. If you are a bit new to this, as you said, then just try it now, and then maybe sometime along the way you will learn about proper semantic HTML markup, CSS styling, etc.

+1


a source


Try using this, I am already testing 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>

      

+1


a source


why not display the results on <div>

maybe or <p>

??

0


a source


I believe I \n

should work. You may not have or have the wrong 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 to block the response. You could us <div>

, and you could even style it to look like <textarea>

(or even better!)

0


a source


It took a while, but after a while I was able to pinpoint those two locations (lines 154 and 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 stripped the tags in my textarea box.

0


a source


I solved my problem using the strip_tags function. when a user edited a previous post, it was saved
to the database and displayed on the blog. But the strip_tags function takes care of that.

0


a source







All Articles