2009-12-25 45 views
3

我有一個文本區域,我想採取文本區域的輸入並將它合併在一起。一切工作正常,只是它逃避報價。例如test's被輸出爲test/'sphp htmlentities解碼textarea

爲了解決這個問題我試圖htmlenttries如,

<?php $inputtext= $_POST['textinput']; 
     $encodetext = htmlentities($inputtext); 
     $finaltext = html_entity_decode($encodetext); 

     echo '<p>'.$finaltext .'</p>'; ?> 

這應根據html_entity_decode手冊(除非我讀錯這可能很可能是這種情況)

工作
+0

你得到 「測試/ 's」 或 「測試\' s」 嗎?此外,您發佈的代碼不會打印轉義引號。 – outis 2009-12-25 10:20:50

+2

你有任何機會啓用魔術報價嗎? – 2009-12-25 10:22:41

+0

你希望通過調用'htmlentities'然後加上'html_entity_decode'完成什麼?它們是相互反轉的,所以'html_entity_decode(htmlentities($ str))== $ str'。 – outis 2009-12-25 10:24:57

回答

6

解決方案可能是爲您提供反斜槓。

當數據來自POST或GET時,會自動添加斜槓。這被稱爲魔術引號,默認情況下已啓用。

您可以通過使用stripslashes()

<?php 

$text = $_POST['txtarea']; // from textarea 
if(get_magic_quotes_gpc()){ 
    $text = stripslashes($text); 
    // strip off the slashes if they are magically added. 
} 
$text = htmlentities($text); 
// what htmlentities here does is really to convert: 
// & to &amp; 
// " to &#039; 
// and change all <and> to &lt; and &gt; respectively. this will automatically disable html codes in the text. 
echo '<pre>'.$text.'</pre>'; 

?> 

見刪除這些斜線:http://php.net/manual/en/function.stripslashes.php

+0

,似乎在伎倆感謝 – BandonRandon 2009-12-26 00:01:23

+0

沒有問題! – mauris 2009-12-26 01:05:57

+0

**注意:**,'get_magic_quotes_gpc()'_always自PHP 5.4.0_ [doc]返回FALSE(http://php.net/manual/en/function.get-magic-quotes-gpc.php#因爲代碼現在應該讀取'if(get_magic_quotes_gpc()|| version_compare(PHP_VERSION,'5.4.0','>')){'。 – nobug 2016-12-14 07:19:52

1

確保您沒有在您撥打htmlentitieshtml_entity_decode的電話中傳遞第二個參數。如果你這樣做,他們會以不同的方式逃避/ unescape報價。檢查htmlentitieshtml_entity_decode文檔中$quote_style參數的說明。

2

您需要使用$encodetext = htmlentities ($inputtext, ENT_QUOTES);,不會試圖逃跑的單引號和雙引號。看看下標誌這裏:htmlentities