Back to Path class

Method clean

public static string
clean
(mixed $path, mixed $ds = DIRECTORY_SEPARATOR)
Function to strip additional / or \ in a path name.
Parameters
  • string $path The path to clean.
  • string $ds Directory separator (optional).
Returns
  • string The cleaned path.
Since
  • 1.7.0
-
  • \UnexpectedValueException
Class: Path
Project: Joomla

Method clean - Source code

/**
 * Function to strip additional / or \ in a path name.
 *
 * @param   string  $path  The path to clean.
 * @param   string  $ds    Directory separator (optional).
 *
 * @return  string  The cleaned path.
 *
 * @throws  \UnexpectedValueException
 * @since   1.7.0
 */
public static function clean($path, $ds = DIRECTORY_SEPARATOR)
{
    if (!\is_string($path) && !empty($path)) {
        throw new \UnexpectedValueException(sprintf('%s() - $path is not a string', __METHOD__));
    }
    $path = trim($path);
    if (empty($path)) {
        $path = JPATH_ROOT;
    } elseif ($ds === '\\' && substr($path, 0, 2) === '\\\\') {
        $path = "\\" . preg_replace('#[/\\\\]+#', $ds, $path);
    } else {
        $path = preg_replace('#[/\\\\]+#', $ds, $path);
    }
    return $path;
}