0

What is the best way to convert a string will XML entities like é to => éin PHP ?

$string = "\xe9"; // é
echo utf8_encode($string );

is working great, but what to do with é

replace &# per \ then use utf8 ?

Rikesh
  • 26,156
  • 14
  • 79
  • 87
TomPAP
  • 249
  • 4
  • 12
  • [html_entity_decode](http://us3.php.net/manual/en/function.html-entity-decode.php) would do the trick. – Jon May 03 '13 at 16:32
  • seems to work only on PHP5.4 http://sandbox.onlinephpfunctions.com/code/2338f4929c669313e91774f97b3fd2f07d4a304e – TomPAP May 03 '13 at 16:45

2 Answers2

4

Prior to PHP 5.4, the encoding defaults to ISO-8859-1

So use:

echo html_entity_decode('é', ENT_COMPAT, 'UTF-8');

Output for 5.0.0 - 5.5.0beta2 (in UTF-8 encoding)

é

Example in 80+ PHP Versions

hakre
  • 193,403
  • 52
  • 435
  • 836
Jon
  • 4,746
  • 2
  • 24
  • 37
2

You could use html_entity_decode.

An IDEone to test with.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Works only on PHP 5.4 not working on PHP5.3, http://sandbox.onlinephpfunctions.com/code/2338f4929c669313e91774f97b3fd2f07d4a304e – TomPAP May 03 '13 at 16:45
  • @TomPAP You didn't mention any particular PHP version. It works, but it's using the wrong encoding to show it on the page. See Jon's answer how to work around it on 5.3 and earlier if you're using UTF-8. – Joachim Isaksson May 03 '13 at 16:51