Posted By

iloveitaly on 05/18/10


Tagged

html utf convert


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Nanobyte


UTF to HTML


 / Published in: PHP
 

Perfect for data that needs to be enclosed in cdata tags and have HTML entities.

Ex: htmlentities(utf8tohtml($string))

  1. function utf8_to_html($data) {
  2. return preg_replace("/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e", '_utf8_to_html("\\1")', $data);
  3. }
  4.  
  5. function _utf8_to_html($data) {
  6. $ret = 0;
  7. foreach((str_split(strrev(chr((ord($data{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($data, 1)))) as $k => $v)
  8. $ret += (ord($v) % 128) * pow(64, $k);
  9. return "&#$ret;";
  10. }

Report this snippet  

You need to login to post a comment.