PHP Tidy:打破textareas

I have a html form page (test.php) with 2 textareas and a submit button. Instead of copying the source code that I want to format and hardcode it everytime, I have created the first textarea so that I can copy the to-be-cleaned code into it. Then upon pressing the submit button, the form posts to the same page and shows the tidified source code in the 2nd text area. Displaying this tidified source code is the problem. Passing the following code through HTML tidy and trying to display the output in a textarea, breaks the output display. What I mean by that is, if you notice, I have </textarea> inside of the following html code. As soon as Tidy encounters it, it's closing textarea and parsing the rest of the info of the source code as if it were a part of the HTML document, thereby breaking my entire html form page (test.php).

SAMPLE SOURCE CODE THAT I AM PASSING TO HTML TIDY:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hi</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">

<h1 data-title="Hi"> <a href="<?php echo $_SERVER['PHP_SELF']; ?>"> Hi </a> </h1>  

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

  <textarea name="comments" id="comments" cols="150" rows="15"><?php echo (isset($_POST['comments']) ? $_POST['comments'] : '' ); ?></textarea>
  <br>
  <input type="submit" name="sbt_process" id="sbt_process" value="Submit">
</form>

<textarea name="css" id="css" cols="150" rows="18"><?php echo (isset($text) ? $text : ''); ?></textarea>

</div><!-- #wrapper -->
</body>
</html>

HTML TIDY PARAMTERS:

$params_body = array(
    'break-before-br'       => 'no',
    'clean'                 => 'clean',
    'doctype'               => 'strict',
    'drop-empty-paras'      => 'yes',
    'drop-font-tags'        => 'yes',
    'force-output'          => 'yes',
    'indent'                => TRUE, //'yes',
    'indent-attributes'     => FALSE,
    'indent-spaces'         => 4,
    'input-encoding'        => 'utf8',
    'join-styles'           => 'yes',
    'literal-attributes'    => 'yes', //This option specifies if Tidy should ensure that whitespace characters within attribute values are passed through unchanged.
    'logical-emphasis'      => 'yes',
    'lower-literals'        => 'yes',
    'merge-divs'            => 'no',
    'merge-spans'           => 'yes',
    'output-encoding'       => 'ascii',
    'output-xhtml'          => 'yes',
    //'output-xml'          => true,
    'output-bom'            => 'no',
    'preserve-entities'     => 'yes',
    'quiet'                 => 'yes',
    'quote-ampersand'       => 'yes',
    'quote-marks'           => 'no',
    'quote-nbsp'            => TRUE, 
    'show-body-only'        => FALSE,
    'show-errors'           => 0,
    'show-warnings'         => 0,
    'sort-attributes'       => 'alpha',
    'vertical-space'        => TRUE, //'yes',
    'wrap'                  => 0, //This option specifies the right margin Tidy uses for line wrapping. Tidy tries to wrap lines so that they do not exceed this length. Set wrap to zero if you want to disable line wrapping.
    'wrap-attributes'       => FALSE,
    'tab-size'              => 20, //This option specifies the number of columns that Tidy uses between successive tab stops. It is used to map tabs to spaces when reading the input. Tidy never outputs tabs.
    'wrap-php'              => 0,
    'wrap-script-literals'  => TRUE,
    'escape-cdata'          => TRUE,
    'indent-cdata'          => TRUE,
    'numeric-entities'      => FALSE,
    'fix-uri'               => FALSE,
    'markup'                => TRUE
);

I have been messing around with the different params of php tidy, but its of no use. I even tried to output it as XML, but that did not help. How can I make Tidy show the cleaned source code in the 2nd textarea correctly without breaking/parsing the </textarea> tag of the source code that I am trying to clean?

The trick is to have the browser not treat the outputted text as tags at all (because you know it REALLY wants to), instead, you can replace < with &lt; and > with &gt;. The browser will run through and it will render out &lt; as <, but because the browser didn't know that was the opening of an HTML tag, we have now tricked our browser into plaintext responses. Give this a shot:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hi</title>
</head>    
<?PHP
$text = "</textarea>What's up?"; //Setting a potentially problematic string.

$text = str_replace("<", "&lt;", $text); //Do a find and replace for the < and save it back to the final variable to be rendered later
$text = str_replace(">", "&gt;", $text); //Do a find and replace for the > and save it back to the final variable to be rendered later.
?>
<body>
<div id="wrapper">
<textarea name="css" id="css" cols="150" rows="18">
<?php echo (isset($text) ? $text : ''); ?>
</textarea>

</div><!-- #wrapper -->
</body>
</html>

If all goes well on your end, this grossly oversimplified example will demonstrate that you can solve all your problems with str_replace() without sacrificing your code and without having to bang your head against a wall. ...My wall has a large dent in it right next to my desk.

This process should still work with tidy, and your POST variables, just gather all the information after the form submit, run it through tidy, and then run that through the str_replace function. That should get it done. Hooray!