Skip to content
Permalink
Browse files

add proper method to cut the string

  • Loading branch information...
saeideng committed Mar 22, 2018
1 parent 43df017 commit f405fdfade475709b0f99ca0a89ddd7c76bbc65b
Showing with 14 additions and 3 deletions.
  1. +13 −3 src/Filesystem/File.php
  2. +1 −0 tests/TestCase/Filesystem/FileTest.php
@@ -355,21 +355,31 @@ public function name()
}
/**
* Returns the file basename. simulate the php basename().
* Returns the file basename. simulate the php basename() for multibyte (mb_basename).
*
* @param string $path Path to file
* @param string|null $ext The name of the extension
* @return string the file basename.
*/
protected static function _basename($path, $ext = null)
{
//check for multibyte string and use basename() if not found
if (mb_strlen($path) === strlen($path)) {
return ($ext===null)? basename($path) : basename($path, $ext);
}
$splInfo = new SplFileInfo($path);
$name = ltrim($splInfo->getFilename(), DS);
if ($ext === null || rtrim($name, $ext) === '') {
if ($ext === null || $ext === '') {
return $name;
}
$ext = preg_quote($ext);
$new = preg_replace("/({$ext})$/u", "", $name);
// basename of '/etc/.d' is '.d' not ''
return ($new === '')? $name : $new;
return rtrim($name, $ext);
}
/**
@@ -176,6 +176,7 @@ public function baseNameValueProvider()
['/نام.txt', '.txt'],
['/نام فارسی.txt', '.txt'],
//
['abcde.ab', '.abe'],
['/etc/sudoers.d', null],
['/etc/.d', '.d'],
['/etc/sudoers.d', '.d'],

0 comments on commit f405fdf

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.