Stop basename() From Changing Russian and Other Non-Latin Strings

Here's another gotcha if you're using PHP's  basename() on a string that contains non-Latin characters: The resulting string will get butchered.

This issue has apparently been fixed, but it still affects up to (at least) PHP 5.5. From the same page, here's a workaround that provides a multibyte equivalent for basename():

private function basename($path, $suffix = null) {
    $split = preg_split('/\//', rtrim($path, '/ '));
    return substr(basename('X' . $split[count($split) - 1], $suffix), 1);
}

Hopefully that will save you some headache if you're wondering why your strings are getting altered!

Author avatar

About the author

Creator of Surreal CMS and other web things. Follow me for tweets about JavaScript, CSS, and web programming.