Skip to content

Instantly share code, notes, and snippets.

@benzittlau
Created November 24, 2012 22:31
Show Gist options
  • Save benzittlau/4141659 to your computer and use it in GitHub Desktop.
Save benzittlau/4141659 to your computer and use it in GitHub Desktop.
Test demonstrating the danger of not explicitly setting your locale in PHP
<?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"; // מחרוזת בדיקה
// 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