2012-07-12 69 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;,它對用戶是可見的,那麼您將得到一些雙重編碼。

相關問題