30
loading...
This website collects cookies to deliver better user experience
"Hi" -> Encode("Hi") -> Send(".... ..") -> Decode(".... ..") -> "Hi"
SOS SOS CQD CQD Titanic. We are sinking fast. Passengers are being put into boats. Titanic
75 / 2 = 37 + 1
37 / 2 = 18 + 1
18 / 2 = 9 + 0
9 / 2 = 4 + 1
4 / 2 = 2 + 0
2 / 2 = 1 + 0
1 / 2 = 0 + 1
1101001 => 1001011
11100010 10011010 10100001
// We first extract the hexadecimal value of a string, like "A"
$value = unpack('H*', "A");
// Convert it now from hexadecimal to decimal (just a number)
$unicodeValue = base_convert($value[1], 16, 10); // $unicodeValue is 65
// Now we transform it from base 10 (decimal) to base 2 (binary)
echo base_convert($unicodeValue, 10, 2); // 1000001
default_charset = "UTF-8"
. To use it, we need to pass the string, a list of valid encodings that you expect to detect, and whether you want a strict comparison (recommended in most cases).
Here's an example of a way to determine whether a string is in UTF-8:
```php
mb_detect_encoding($string, 'UTF-8', true);
mb_detect_encoding($string, "JIS, eucjp-win, sjis-win", true);
$array[] = "ASCII";
$array[] = "JIS";
$array[] = "EUC-JP";
mb_detect_encoding($string, $array, true);
// Convert EUC-JP to UTF-8
$string = mb_convert_encoding($stringInEUCJP, "UTF-8", "EUC-JP");
// Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UTF-8
$string = mb_convert_encoding($str, "UTF-8", "JIS, eucjp-win, sjis-win");
, but since it depends on the underlying implementation, using
```mb_convert_encoding```
is more reliable and consistent.
### Checking that we have the right encoding
Before processing or storing any input, it is a good idea to check that we have the string in the right encoding. To achieve this, we can use
```mb_check_encoding```
, and it'll return true or false. For example, to check that a string is in UTF-8:
```php
mb_check_encoding($string, "UTF-8");
header('Content-Type: text/html; charset=utf-8');
, you might run into this issue. This just means that what PHP was expecting to get as UTF-8 is not in that encoding, so we can solve the problem by converting it first:
```php
$array['name'] = mb_convert_encoding($array['name'], 'UTF-8', 'UTF-8');
T�léawe