Converting extended ASCII characters (128-159) into HTML entities?

How does one convert extended ASCII characters, codes 128+, into HTML entities? I tried the following:

for($i=0;$i<=255;$i++){
	echo "$i: ".htmlentities(chr($i))."<br />\
";
}

However for character codes 128 to 159, I got funny characters as output instead of an HTML entity.

The output probably just looks funny because your page encoding isn’t set correctly. Add a <meta> tag with encoding=utf8 and refresh.

I added an

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Tag, but I still get the funny characters.

But I don’t think that should be the issue anyway - I’d like for instance the chr(128) character to be encoded as the entity “€”. But the funny characters are present in the source code for the page too, and not the entity.

“Extended ASCII” is vague. The characters that aren’t working are actually in Microsoft’s cp1252/Windows-1252 charset, so you need to pass “cp1252” for the charset parameter to htmlentities(). By default, htmlentities() uses “ISO-8859-1,” which is without Microsoft’s extensions (the characters that you are having issues with). Both cp1252 and ISO-8859-1 can be considered “extended ASCII,” and arguably many more charsets can be too.

Thanks - that seems to have done the trick :slight_smile:

for($i=0;$i<=255;$i++){
	echo "$i: ".htmlentities(chr($i), ENT_QUOTES, 'cp1252')."<br />\
";
}