php - Why does a string that contains HTML entity not displaying character after being ran through htmlspecialchars() -


i have string of text pulled sql , ran through php strip_tags , htmlspecialchars because need remove html formatting user might try add. displayed in textarea uneditable div. textarea shows raw html entity code (eg. & <) , that's want. div, preview of textarea content, want show actual character (eg. & <). so, need div convert special characters html entities want current html entities displayed characters.

this string of text contain quite few different characters because technical writings restaurant equipment it's not ampersands , quotation marks. basically, there enough making list not easy option.

this function run string through both textarea , preview div:

function removetags($data) {     $data = strip_tags($data);     $data = htmlspecialchars($data, ent_html5, 'utf-8');     return $data; } 

this textarea displays:

this unit has ability lower food temperature 160&#176;f 38&#176;f, 110 lbs. 

an unfortunately preview div shows same information want preview div show line below while still removing html tags converting special characters html entities:

this unit has ability lower food temperature 160°f 38°f, 110 lbs.

the htmlspecialchars() function converts characters such & , turns them html entities, such &amp;.

also, if want convert characters, , not ones htmlspecialchars() converts, use htmlentities() instead.

then can use str_replace() function show html entities.

for example:

$data = "this unit has ability lower food temperature 160°f 38°f, 110 lbs.";  $data = "this unit has ability lower food temperature 160°f 38°f, 110 lbs.";  function removetags($data) {     $data = strip_tags($data);     $data = htmlentities($data);     return $data; }  echo '<textarea cols="95" rows="6">' . str_replace('&', '&amp;', removetags($data)) . '</textarea>'; echo '<div>' . removetags($data) . '</div>'; 

in textarea, output is:

this unit has ability lower food temperature 160&deg;f 38&deg;f, 110 lbs. 

in div, output is:

this unit has ability lower food temperature 160°f 38°f, 110 lbs. 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -