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

New Hampshirite building web apps in Florida. Creator of Surreal CMS, Postleaf, and DirtyMarkup.

Need to get in touch? Catch me on Twitter.