Skip to content

Instantly share code, notes, and snippets.

@MartinSadovy
Last active December 12, 2015 12:28
Show Gist options
  • Save MartinSadovy/4771705 to your computer and use it in GitHub Desktop.
Save MartinSadovy/4771705 to your computer and use it in GitHub Desktop.
Windows uses the local character encoding (cp125*) for file and directory name.
<?php
function utf8ToLocalEncoding($s) {
if (substr(PHP_OS, 0, 3) === 'WIN') {
$tmp = explode(".", setlocale(LC_CTYPE, 0));
if (isset($tmp[1]) && is_numeric($tmp[1]))
$s = @iconv("UTF-8", "cp" . $tmp[1] . "//TRANSLIT", $s);
}
return $s;
}
function localEncodingToUtf8($s) {
if (substr(PHP_OS, 0, 3) === 'WIN') {
$tmp = explode(".", setlocale(LC_CTYPE, 0));
if (isset($tmp[1]) && is_numeric($tmp[1]))
$s = @iconv("cp" . $tmp[1] . "//TRANSLIT", "UTF-8", $s);
}
return $s;
}
$original = "ěščřžýáíé";
$a = utf8toLocalEncoding($original);
$b = localEncodingToUtf8($a);
var_dump($original, $a, $b, $original === $b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment