function convert($str) {
$str = mb_convert_encoding($str , 'UTF-32', 'UTF-8'); // Big endian
$t = unpack('N*', $str);
$t = array_map(function($n) { return "&#$n;"; }, $t);
return join('', $t);
}
var_dump(convert('foo bar')); // string(39) "foo bar"