本文介绍了使用HTML-ENTITIES charset替代mb_convert_encoding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  mb_convert_encoding($ string,'HTML-ENTITIES','utf-8') ; 

我需要一个替代代码,它完全一样,但不使用任何mb_ *函数在某些环境下,mb扩展名不可用。)



我认为

  utf8_decode(htmlentities($ string,ENT_COMPAT,'utf-8')); 

应该完全一样,但不幸的是它不会。

解决方案

我玩了一下,发现这很有趣。它似乎第二部分也运行htmlspecialchars。必须是mb_convert_encoding中的一些错误,因为htmlentities不能正确运行。



如果在结果中运行htmlspecialchars_decode,则与使用mb_convert_encoding完全相同。 / p>

代码:

  $ string ='Test:! %& /()=ÖÄÜöäü<'; 
echo mb_convert_encoding($ string,'HTML-ENTITIES','utf-8')。\\\
\\\
;
echo htmlspecialchars_decode(utf8_decode(htmlentities($ string,ENT_COMPAT,'utf-8',false)));

这里是上面代码的演示:



在这里没有htmlspecialchars_decode的演示,显示你的问题:


I have the following code:

mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8');

I need to have an alternative code which does exactly the same but does not use any mb_* functions (the mb extension is not available on some environments).

I thought that

utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8'));

should do exactly the same, but unfortunately it does not.

解决方案

I played around a bit, and find this very interesting. It seems like the second part also runs "htmlspecialchars". Must be some bug in mb_convert_encoding, as htmlentities is not run correctly.

If you run htmlspecialchars_decode over the result, you get exactly the same as if you would use mb_convert_encoding.

The code:

$string = 'Test:!"$%&/()=ÖÄÜöäü<<';
echo mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8')."\n\n";
echo htmlspecialchars_decode(utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8', false)));

Here a demo of the code above:http://sandbox.onlinephpfunctions.com/code/715acade3b8337d9c9e48e58deee2a237015c259

And here a demo without htmlspecialchars_decode, to show your problem:http://sandbox.onlinephpfunctions.com/code/5c4a32bf99aa8fd6246c4a77132a023d32945363

这篇关于使用HTML-ENTITIES charset替代mb_convert_encoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:01