I am trying to convert HTML entities from a source string to their literal character equivalent.
For example:
<?php
$string = "Hello – 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);
Definition and Usage. The html_entity_decode() function converts HTML entities to characters. The html_entity_decode() function is the opposite of htmlentities().
?> Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities.
The htmlspecialchars_decode() function converts some predefined HTML entities to characters. HTML entities that will be decoded are: & becomes & (ampersand) " becomes " (double quote)
The htmlentities() function converts characters to HTML entities. Tip: To convert HTML entities back to characters, use the html_entity_decode() function. Tip: Use the get_html_translation_table() function to return the translation table used by htmlentities().
–
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');
Try using charset
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php
$string = "Hello – World";
$converted = html_entity_decode($string , ENT_COMPAT, 'UTF-8');
echo $converted;
?>
This should work And it should be converted also in the source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With