2012-07-12 71 views
2

我的问题类似于this的问题,但我没有使用代码点火器。我将从数据库中获取的变量回显到文本输入的值属性中。该变量可能包含“或”或其他任何特殊字符在文本输入的值属性中使用htmlspecialchars

我想:

<input type="text" name="myTextInput" value="<?= htmlspecialchars($dbValue, ENT_QUOTES); ?>" /> 

但它输出的报价为&quot;&#039;这是不是我想要什么,我想要的文字输入实际上包含。用户输入的报价

我应该使用php函数还是使用javascript函数来转义字符串?如果我不逃避它,我会得到一个javascript错误,因为$ dbValue字符串内部的引号正在交互与价值属性引用。

+0

同样的问题。我解决了只使用htmlspecialchars($值);即使在é,ô,ñ与''和“ – Peter 2015-11-20 13:57:10

回答

3

你会想要使用html_entity_decode。下面是该文件的例子:

<?php 
$orig = "I'll \"walk\" the <b>dog</b> now"; 

$a = htmlentities($orig); 

$b = html_entity_decode($a); 

echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now 

echo $b; // I'll "walk" the <b>dog</b> now 
?> 

参考http://www.php.net/manual/en/function.html-entity-decode.php

5

这正是你想要的,但是。例如

如果你插入的数据是

Davy "Dead Pirate" Jones 

,你插入到一个输入字段从字面上看,你会最终

<input type="text" name="..." value="Davy "Dead Pirate" Jones" /> 

将被interepreted如下:

<input> field with attributes: 
    text -> 'text' 
    name -> '...' 
    value -> ' ' (a single space) 
    Dead -> 
    Pirate -> 
    " ? danging quote 
    Jones -> 
    " ? -> another dangling quote 

通过比较,做了一个html_entities之后,你会有

Davy &quot;Dead Pirate&quot; Jones 

并且可以插入<input>字段没有问题。

如果输入字段的值包含文字&quot;,它对用户是可见的,那么您将得到一些双重编码。

相关问题