Htmlentities () double encoding objects in a string

I want only uncoded characters to be converted to html objects without affecting entities that are already present. I have a string that previously encoded objects, for example:

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

When I use htmlentities() , & at the beginning of entities is encrypted again. This means ‐ and other objects have & encoded to & :

 × 

I tried to decode the complete string, and then coded it again, but it does not 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 correctly. Is there a way to prevent or stop this?

+6
source share
2 answers

Set the optional variable $double_encode to false . See the documentation for more information.

The code you received should look like this:

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

You looked at the documentation well, but you missed the best part. Sometimes it can be difficult to decrypt:

 // > > > > > > 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. Mixing. Usually I ignore the signature line and move on to the next block ( Parameters ) for advertising messages for each argument.

So, you want to use the double_encoded argument at the end to tell htmlentities not to transcode (and you probably want to stick with UTF-8 if you have no specific reason not to):

 $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

+4
source

All Articles