user998163
user998163

Reputation: 483

Cannot decode HTML entity in PHP

I want this string:

"Hartnäckigkeit zahlt sich aus"

Getting converted to this:

Hartnäckigkeit zahl sich aus

I tried this:

html_entity_decode( "Hartnäckigkeit zahlt sich aus", ENT_COMPAT, 'UTF-8')

But did not succeed.

Upvotes: 1

Views: 1038

Answers (3)

mhall
mhall

Reputation: 3701

Your encoded string seems off from the beginning, and may have been created somewhere by wrongfully HTML-encoding an UTF-8 string as ISO-8859-1:

Example (source code in UTF-8 format):

echo htmlentities(
    "Hartnäckigkeit zahlt sich aus", ENT_COMPAT, 'ISO-8859-1'
), PHP_EOL;

Output:

Hartnäckigkeit zahlt sich aus

(same as Hartnäckigkeit zahlt sich aus)

Use this to decode it:

echo html_entity_decode(
    "Hartnäckigkeit zahlt sich aus",
    ENT_COMPAT,
    'ISO-8859-1'
);

Output:

Hartnäckigkeit zahlt sich aus

Upvotes: 2

Justin Smith
Justin Smith

Reputation: 46

I got your output doing this:

$test = "Hartnäckigkeit zahlt sich aus";
echo html_entity_decode($test, ENT_COMPAT, "UTF-8");

Your entity codes seem off.

Upvotes: 0

ErasmoOliveira
ErasmoOliveira

Reputation: 1426

You can use utf8_encode ( $data );

See more about encode here

http://php.net/manual/en/function.utf8-encode.php

Upvotes: -1

Related Questions