2009-12-25 47 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