在这里,我尝试使用单个函数:
function isAbsolutePath($path) {
if (!is_string($path)) {
$mess = sprintf('String expected but was given %s', gettype($path));
throw new \InvalidArgumentException($mess);
}
if (!ctype_print($path)) {
$mess = 'Path can NOT have non-printable characters or be empty';
throw new \DomainException($mess);
}
// Optional wrapper(s).
$regExp = '%^(?(?:[[:print:]]{2,}://)*)';
// Optional root prefix.
$regExp .= '(?(?:[[:alpha:]]:/|/)?)';
// Actual path.
$regExp .= '(?(?:[[:print:]]*))$%';
$parts = [];
if (!preg_match($regExp, $path, $parts)) {
$mess = sprintf('Path is NOT valid, was given %s', $path);
throw new \DomainException($mess);
}
if ('' !== $parts['root']) {
return true;
}
return false;
}
我从我的一个项目中获取了这个,你在使用文件和路径时可能会觉得很有用:
dragonrun1/file_path_normalizer