PHP code example of jackiedo / path-helper

1. Go to this page and download the library: Download jackiedo/path-helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jackiedo / path-helper example snippets


use Jackiedo\PathHelper\Path;

// ...

$helper = new Path;
$return = $helper->doSomething();

/**
 * Formats the directory separators of a given path with a specific string.
 *
 * @param string $path      the path want to normalize
 * @param string $separator the directory separator want to use
 *
 * @return string
 */
public static function normalize($path, $separator = DIRECTORY_SEPARATOR);

$return1 = Path::normalize('path\\to/specific/file/or\\directory');
// The result returned will depend on the operating system
//     On Windows -> path\to\specific\file\or\directory
//     On Unix    -> path/to/specific/file/or/directory

$return2 = Path::normalize('path\\to/specific/file/or\\directory', '/');
// path/to/specific/file/or/directory

$return3 = Path::normalize('path\\to/specific/file/or\\directory', ' > ');
// path > to > specific > file > or > directory

/**
 * Normalize directory separators of a given path according to Unix OS style.
 *
 * @param string $path the path want to normalize
 *
 * @return string
 */
public static function unixStyle($path);

$return = Path::unixStyle('path\\to/specific/file/or\\directory');
// path/to/specific/file/or/directory

/**
 * Normalize directory separators of a given path according to Windows OS style.
 *
 * @param string $path the path want to normalize
 *
 * @return string
 */
public static function winStyle($path);

$return = Path::winStyle('path\\to/specific/file/or\\directory');
// path\to\specific\file\or\directory

/**
 * Normalize directory separators of a given path according to the current OS style.
 *
 * @param string $path the path want to normalize
 *
 * @return string
 */
public static function osStyle($path);

$return = Path::osStyle('path\\to/specific/file/or\\directory');
// The result returned will depend on the operating system
//     On Windows -> path\to\specific\file\or\directory
//     On Unix    -> path/to/specific/file/or/directory

/**
 * Return absolute path from a given path.
 *
 * This method is an alternative to `realpath()` function for non-existent paths.
 *
 * @param string $path      the path want to format
 * @param string $separator the directory separator want to use in the result
 *
 * @return string
 */
public static function absolute($path, $separator = DIRECTORY_SEPARATOR);

$return = Path::absolute('./this\\is/../sample/path');
// The result returned will depend on the operating system and current working directory
// You will probably get the following result: /home/www/public_html/this/sample/path

/**
 * Return relative path from a given file or directory to another location.
 *
 * @param string $from      the path of departure file or directory
 * @param string $to        the path of destination file or directory
 * @param string $separator the directory separator want to use in the result
 *
 * @return string
 */
public static function relative($from, $to, $separator = DIRECTORY_SEPARATOR);

$return = Path::absolute('./this\\is/../sample/path', '/home/www/another/directory');
// The result returned will depend on the operating system and current working directory
// You will probably get the following result: ../../../../another/directory

/**
 * Check if a given path is an absolute path.
 *
 * @param string $path the path want to check
 *
 * @return bool
 */
public static function isAbsolute($path);

$return = Path::isAbsolute('/home/www/public_html');      // true
$return = Path::isAbsolute('sample/../path');             // false
$return = Path::isAbsolute('D:\\home\\www\\public_html'); // true
$return = Path::isAbsolute('sample\\..\\path');           // false

/**
 * Check if a given path is a relative path.
 *
 * @param string $path the path want to check
 *
 * @return bool
 */
public static function isRelative($path);

$return = Path::isRelative('/home/www/public_html');      // false
$return = Path::isRelative('sample/../path');             // true
$return = Path::isRelative('D:\\home\\www\\public_html'); // false
$return = Path::isRelative('sample\\..\\path');           // true

/**
 * Check if a given path is ancestor of the another path.
 *
 * Return true if the input path is ancestor of the comparison
 *
 * @param string $path       the path want to check
 * @param string $comparison the target path used for comparison
 *
 * @return bool
 */
public static function isAncestor($path, $comparison);

$return = Path::isAncestor('/home/www', '/home/www/public/assets/css/../images/sample.png');     // true
$return = Path::isAncestor('/home/www', '/another_home/public/assets/css/../images/sample.png'); // false

/**
 * Check if a given path is descendant of the another path.
 *
 * Return true if the input path is descendant of the comparison
 *
 * @param string $path       the path want to check
 * @param string $comparison the target path used for comparison
 *
 * @return bool
 */
public static function isDescendant($path, $comparison);

$return = Path::isDescendant('/home/www/public/assets/css/../images/sample.png', '/home/www');     // true
$return = Path::isDescendant('/another_home/public/assets/css/../images/sample.png', '/home/www'); // false