2012-04-08 17 views
1

我有一個HTML表單頁面(test.php),有2個textareas和一個提交按鈕。我不是每次都要複製要格式化和硬編碼的源代碼,而是創建了第一個textarea,以便我可以將待清理的代碼複製到其中。然後在按下提交按鈕後,表單將發佈到同一頁面,並在第二個文本區域中顯示已更新的源代碼。 顯示這個更加完整的源代碼是問題。通過HTML tidy傳遞以下代碼並嘗試在textarea中顯示輸出,從而中斷輸出顯示。我的意思是,如果你注意到,我在下面的html代碼中有</textarea>。只要Tidy遇到它,它就會關閉textarea,並將源代碼的其餘部分解析爲HTML文檔的一部分,從而打破了我的整個HTML表單頁面(test.php)。PHP Tidy:Breaks textareas

示例源代碼,我傳遞給HTML精簡:

<!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精簡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 
); 

我一直在用PHP整齊的不同PARAMS瞎搞,但它沒用。我甚至試圖將它輸出爲XML,但這沒有幫助。我如何使Tidy在第二個textarea中正確顯示已清理的源代碼而不破壞/解析我正在嘗試清理的源代碼的</textarea>標記?

回答

0

訣竅是讓瀏覽器不要把輸出的文本在所有標籤(因爲你知道它真的想),相反,你可以用&lt;>&gt;取代<。 瀏覽器將運行並將呈現&lt;作爲<,但由於瀏覽器不知道這是HTML標記的打開,我們現在已經欺騙了我們的瀏覽器明文響應。這給一個鏡頭:

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

如果所有對你的最終順利的話,這嚴重過於簡單的例子證明你能解決str_replace()函數的所有問題,而犧牲你的代碼,而無需針對碰你的頭壁。 ...我的牆上有一個很大的凹痕,就在我的辦公桌旁邊。

這個過程應該仍然適用於整潔和你的POST變量,只是收集表單提交後的所有信息,通過整齊運行它,然後通過str_replace函數運行它。這應該完成它。萬歲!