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 – World";
$converted = html_entity_decode($string , ENT_COMPAT, 'UTF-8');
echo $converted;
?>
This should work And it should be converted also in the source
Author by
mootymoots
Updated on July 09, 2022Comments
-
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 – 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 over 13 yearsI still get the entity when I view source on that one...?
-
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 over 13 yearsit's converted on the page - but not in the source...? Looking in chrome
-
mootymoots over 13 yearsJust 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 over 13 yearsScratch that comment, you're using APNS. So that means your alert view is displaying
–
as well, right? -
mootymoots over 13 yearsexactly :) That's what I'm trying to fix :)
-
BoltClock over 13 years@mootymoots: What's the output of
$payload
as JSON? -
mootymoots over 13 years{"aps":{"alert":"Hello – World","badge":1,"sound":"default"}}
-
BoltClock over 13 yearsWow, 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 over 13 yearsIt'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 over 13 yearsAnd it was only mental in a browser because I didnt use the right charset for the HTML page... damn!
-
BoltClock over 13 years@mootymoots: There's the problem :) Glad you got it sorted.