Example #1
0
File: FS.php Project: jbzoo/utils
 /**
  * Find relative path of file (remove root part)
  *
  * @param string      $filePath
  * @param string|null $rootPath
  * @param string      $forceDS
  * @param bool        $toRealpath
  * @return mixed
  */
 public static function getRelative($filePath, $rootPath = null, $forceDS = DIRECTORY_SEPARATOR, $toRealpath = true)
 {
     // Cleanup file path
     if ($toRealpath && !self::isReal($filePath)) {
         $filePath = self::real($filePath);
     }
     $filePath = self::clean($filePath, $forceDS);
     // Cleanup root path
     $rootPath = $rootPath ?: Sys::getDocRoot();
     if ($toRealpath && !self::isReal($rootPath)) {
         $rootPath = self::real($rootPath);
     }
     $rootPath = self::clean($rootPath, $forceDS);
     // Remove root part
     $relPath = preg_replace('#^' . preg_quote($rootPath) . '#i', '', $filePath);
     $relPath = ltrim($relPath, $forceDS);
     return $relPath;
 }