-
-
Save jaysonmulwa/45ef189c83812709d60c0a4973550bfb to your computer and use it in GitHub Desktop.
ASCII Space vs UTF-8 Thin Space in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** See it live: https://onlinephp.io/c/551a0 **/ | |
$helloWorld = [ | |
'ascii' => 'Hello, World!', | |
'utf-8' => 'Hello, World!', | |
]; | |
$spacePos = strpos($helloWorld['ascii'], ' '); | |
$outputHex = function (string $string): string { | |
$hex = bin2hex($string); | |
$hexParts = str_split($hex, 2); | |
$hexString = implode(' ', $hexParts); | |
$tab = strlen($string) < 4 ? "\t\t" : "\t"; | |
return sprintf("|%s|$tab%s", $string, $hexString); | |
}; | |
foreach ($helloWorld as $encoding => $hello) { | |
echo strtoupper($encoding) . ": \"$hello\"\n"; | |
$parts = [ | |
$outputHex(mb_substr($hello, 0, $spacePos)), | |
$outputHex(mb_substr($hello, $spacePos, 1)), | |
$outputHex(mb_substr($hello, $spacePos + 1)), | |
]; | |
print_r($parts); | |
} | |
/* Output: | |
ASCII: "Hello, World!" | |
Array | |
( | |
[0] => |Hello,| 48 65 6c 6c 6f 2c | |
[1] => | | 20 | |
[2] => |World!| 57 6f 72 6c 64 21 | |
) | |
UTF-8: "Hello, World!" | |
Array | |
( | |
[0] => |Hello,| 48 65 6c 6c 6f 2c | |
[1] => | | e2 80 89 | |
[2] => |World!| 57 6f 72 6c 64 21 | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment