簡體   English   中英

htmlentities()在字符串中雙重編碼實體

[英]htmlentities() double encoding entities in string

我只希望將未編碼的字符轉換為html實體,而不會影響已存在的實體。 我有一個以前編碼實體的字符串,例如:

gaIUSHIUGhj>‐ hjb×jkn.jhuh>hh> …

當我使用htmlentities() ,實體的開始處的&再次被編碼。 這意味着‐ 和其他實體有&編碼到&

×

我嘗試解碼完整的字符串,然后再次編碼,但似乎沒有正常工作。 這是我試過的代碼:

header('Content-Type: text/html; charset=iso-8859-1');
...

$b = 'gaIUSHIUGhj>‐ hjb×jkn.jhuh>hh> …';
$b = html_entity_decode($b, ENT_QUOTES, 'UTF-8');
$b = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $b);
$b = htmlentities($b, ENT_QUOTES, 'UTF-8'); 

但它似乎沒有正確的方式。 有沒有辦法防止或阻止這種情況發生?

將可選的$double_encode變量設置為false 有關更多信息,請參閱文檔

生成的代碼應如下所示:

$b = htmlentities($b, ENT_QUOTES, 'UTF-8', false);

你很好看文檔 ,但你錯過了最好的部分。 有時可能很難破譯這個:

//     >    >    >    >    >    >    Scroll    >>>    >    >    >    >    >     Keep going.    >    >    >    >>>>>>  See below.  <<<<<<
string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' [, bool $double_encode = true ]]] )

看看最后。

我知道。 混亂。 我通常會忽略簽名行,然后直接進入下一個塊( Parameters ),查看每個參數上的blurb。

所以你想在最后使用double_encoded參數告訴htmlentities不要重新編碼(你可能想要堅持使用UTF-8除非你有特殊的理由不這樣做):

$str = "gaIUSHIUGhj>&hyphen; hjb&times;jkn.jhuh>hh> &hellip;";

// Double-encoded!
echo htmlentities($str, ENT_COMPAT, 'utf-8', true) . "\n";

// Not double-encoded!
echo htmlentities($str, ENT_COMPAT, 'utf-8', false);

https://ignite.io/code/513ab23bec221e4837000000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM