html_entity_decode problem in PHP?

41,659

Solution 1

– maps to a UTF-8 character (the em dash) so you need to specify UTF-8 as the character encoding:

$converted = html_entity_decode($string, ENT_COMPAT, 'UTF-8');

Solution 2

Try using charset

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<?php
$string = "Hello &#8211; World";
$converted = html_entity_decode($string , ENT_COMPAT, 'UTF-8');
echo $converted;
?>

This should work And it should be converted also in the source

Share:
41,659
mootymoots
Author by

mootymoots

Updated on July 09, 2022

Comments

  • mootymoots
    mootymoots almost 2 years

    I am trying to convert HTML entities from a source string to their literal character equivalent.

    For example:

    <?php
    
    $string = "Hello &#8211; World";
    $converted = html_entity_decode($string);
    
    ?>
    

    Whilst this rightly converts the entity on screen, when I look at the HTML code it is still showing the explicit entity. I need to change that so that it literally converts the entity as I am not using the string within an HTML page.

    Any ideas on what I am doing wrong?

    FYI I am sending the converted string to Apple's Push notification service:

    $payload['aps'] = array('alert' => $converted, 'badge' => 1, 'sound' => 'default');
    $payload = json_encode($payload);
    
  • mootymoots
    mootymoots over 13 years
    I still get the entity when I view source on that one...?
  • BoltClock
    BoltClock over 13 years
    @mootymoots: I tested it, I got the raw character instead of the entity. Wonder what else could be causing it... the HTML document's encoding perhaps?
  • mootymoots
    mootymoots over 13 years
    it's converted on the page - but not in the source...? Looking in chrome
  • mootymoots
    mootymoots over 13 years
    Just to add, the PHP is sending it via json_encode to Apple, it's not actually needed to be viewed in browser, it's just helping me debug. It comes through as the entity on the device.
  • BoltClock
    BoltClock over 13 years
    Scratch that comment, you're using APNS. So that means your alert view is displaying &#8211; as well, right?
  • mootymoots
    mootymoots over 13 years
    exactly :) That's what I'm trying to fix :)
  • BoltClock
    BoltClock over 13 years
    @mootymoots: What's the output of $payload as JSON?
  • mootymoots
    mootymoots over 13 years
    {"aps":{"alert":"Hello &#8211; World","badge":1,"sound":"default"}}
  • BoltClock
    BoltClock over 13 years
    Wow, that's very strange. That string is supposed to read "Hello \u2264 World". Any chance you might be overwriting $converted somehow between the decoding and the array? Maybe posting the full script would help... unless that's your full script.
  • mootymoots
    mootymoots over 13 years
    It's fixed. I was using htmlentities() on the source string as without it (in the browser) things went mental. When I removed that and sent to Apple it works fine, Just terrible in a browser :)
  • mootymoots
    mootymoots over 13 years
    And it was only mental in a browser because I didnt use the right charset for the HTML page... damn!
  • BoltClock
    BoltClock over 13 years
    @mootymoots: There's the problem :) Glad you got it sorted.