简体   繁体   English

htmlentities()在字符串中双重编码实体

[英]htmlentities() double encoding entities in string

I want only the unencoded characters to get converted to html entities, without affecting the entities which are already present. 我只希望将未编码的字符转换为html实体,而不会影响已存在的实体。 I have a string that has previously encoded entities, eg: 我有一个以前编码实体的字符串,例如:

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

When I use htmlentities() , the & at the beginning of entities gets encoded again. 当我使用htmlentities() ,实体的开始处的&再次被编码。 This means ‐ 这意味着‐ and other entities have their & encoded to & 和其他实体有&编码到& :

×

I tried decoding the complete string, then encoding it again, but it does not seem to work properly. 我尝试解码完整的字符串,然后再次编码,但似乎没有正常工作。 This is the code I tried: 这是我试过的代码:

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'); 

But it does not seem to work the right way. 但它似乎没有正确的方式。 Is there a way to prevent or stop this from happening? 有没有办法防止或阻止这种情况发生?

Set the optional $double_encode variable to false . 将可选的$double_encode变量设置为false See the documentation for more information. 有关更多信息,请参阅文档

Your resulting code should look like: 生成的代码应如下所示:

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

You did good looking at the documentation , but you missed the best part. 你很好看文档 ,但你错过了最好的部分。 It can be hard to decipher this sometimes: 有时可能很难破译这个:

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

Look at the very end. 看看最后。

I know. 我知道。 Confusing. 混乱。 I usually ignore the signature line and go straight down to the next block ( Parameters ) for the blurbs on each argument. 我通常会忽略签名行,然后直接进入下一个块( Parameters ),查看每个参数上的blurb。

So you want to use the double_encoded argument at the end to tell htmlentities not to re-encode (and you probably want to stick with UTF-8 unless you have a specific reason not to): 所以你想在最后使用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 https://ignite.io/code/513ab23bec221e4837000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM