Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html_entity_decode problem in PHP?

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);
like image 565
mootymoots Avatar asked Jan 09 '11 10:01

mootymoots


People also ask

What is Html_entity_decode?

Definition and Usage. The html_entity_decode() function converts HTML entities to characters. The html_entity_decode() function is the opposite of htmlentities().

What's the difference between HTML entities () and htmlspecialchars ()?

?> 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.

How to decode HTML tag in PHP?

The htmlspecialchars_decode() function converts some predefined HTML entities to characters. HTML entities that will be decoded are: &amp; becomes & (ampersand) &quot; becomes " (double quote)

How to use HTML entities in PHP?

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().


2 Answers

&#8211; 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');
like image 190
BoltClock Avatar answered Oct 02 '22 12:10

BoltClock


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

like image 32
mr.Shu Avatar answered Oct 02 '22 12:10

mr.Shu