If you are not confident that your program will properly lead the way to the required file or directory - use Path2, it automatically converts all slashes to those which are used in the current operating system, yet so over also will trim excess slashes if there are in the path.
To install this library - run the command below in your terminal:
composer require maximgrynykha/path2
<?php
use Path2\Path;
require_once 'vendor/autoload.php';
$path = new Path();
// For example, normalize some kinky path
$normalized = $path->to('/\/src/\\\Path2/\/\/\Path.php');
dd($normalized); // "{CWD}/src/Path2/Path.php" (on an UNIX) || "{CWD}\src\Path2\Path.php" (on a Windows)
// Note, by default if any preceding to the main path (first argument),
// from-path (second argument) isn't passed then Path::to()
// uses CWD (current working directory) as a preceding, from-path.
Param | Argument | Example |
---|---|---|
path |
[string]: any path to the file or dir | |
from |
[string]: any path preceding before the main path | CWD (current working directory) |
__DIR__, __NAMESPACE__, etc. |
If you have a problem that cannot be solved using this library, please write your solution, and if you want to help other developers who also use this library (or if you want to keep your solution working after a new version is released, which will be in the package manager dependencies) — create a pull-request. I will be happy to add your excellent code to the library!
🐞 Report any bugs or issues you find on the GitHub issues.
The MIT License (MIT). Please see License File for more information.