Use PHP htmlentities to convert special characters to their entity number rather than entity name

493 Views Asked by At

How do you force PHP "htmlentities" to convert a special character to the entity number rather than to the entity name?

For example, convert É to &#201 rather than to &Eacute

I have searched with no answers.

1

There are 1 best solutions below

0
On

You don't can force PHP "htmlentities" to convert a special character to the entity number rather than to the entity name. Take a user function.

function htmlEntityNumber($char){
  $utf32char = mb_convert_encoding($char,"UTF-32BE","UTF-8");
  return "&#".unpack("N",$utf32char)[1] . ";";
}

//var_dump and echo only show an É
debug::write(htmlEntityNumber('É'));  
//string(5) ASCII   "&#201"