Example 1 — Convert UTF-8 to Latin-1
A UTF-8 letter such as é takes two bytes, but in Latin-1 it takes one. This example hides the PHP 8.2 deprecation notice so the output stays clean. Save the file as UTF-8.
<?php
error_reporting(E_ALL & ~E_DEPRECATED);
$utf8 = "café";
$latin = utf8_decode($utf8);
echo strlen($utf8), PHP_EOL;
echo strlen($latin), PHP_EOL; How It Works
- Start with UTF-8. The letter é uses two bytes, so "café" is 5 bytes.
- Decode. utf8_decode() converts it to Latin-1.
- One byte per letter. Now the string is 4 bytes.
- Compare lengths. The two
strlen()calls show 5 and 4.

