Created
November 24, 2012 22:31
-
-
Save benzittlau/4141659 to your computer and use it in GitHub Desktop.
Test demonstrating the danger of not explicitly setting your locale in PHP
This file contains 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 | |
// Set the locale to 'C' | |
setlocale(LC_CTYPE, 'C'); | |
// Set the string to 'Test Folder' in hebrew | |
$path = '/Hebrew Test Folder/מחרוזת בדיקה'; | |
// Echo out just the base string | |
echo $path . "\n"; // /Hebrew Test Target/מחרוזת בדיקה | |
// Echo the basename of the string and get a | |
// truncated result | |
echo basename($path) . "\n"; // בדיקה | |
// Set the locale for CTYPE to UTF-8 | |
setlocale(LC_CTYPE, "en_US.UTF-8"); | |
// With the updated locale get the expected basename result | |
echo basename($path) . "\n"; // מחרוזת בדיקה |
This file contains 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
// Output of checking the staging locale | |
php > var_dump(setlocale(LC_ALL, '0')); | |
string(170) "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C" | |
// Output of checking the production locale | |
php > var_dump(setlocale(LC_ALL, '0')); | |
string(1) "C" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment