简体   繁体   中英

Convert UTF-8 text to ASCII in PHP

This code outputs UTF-8:

echo mb_detect_encoding("ø")

And this code outputs ASCII:

echo mb_detect_encoding("ø");

So, how do you convert UTF-8 to ASCII? For example: convert ø to &#248

ø is an HTML entity . It uses only ASCII characters, so it's detected as ASCII, yes. You just need to HTML encode your text:

echo htmlentities('ø', ENT_COMPAT, 'UTF-8');

This will output ø , but it's an equivalent named HTML entity.

I'm going out on a limb though and suggest that you don't really understand what you want exactly. Maybe you should read What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and go from there.

Use htmlentities to achieve what you looking for

$myVar = 'ø';

$val = htmlentities($myVar);
echo $myVar;
// ø
echo mb_detect_encoding($myVar);
// UTF-8

echo $val;
// ø
echo mb_detect_encoding($val);
//ASCII

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM