3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * * Find the relative file system path between two file system paths * * @param string $frompath Path to start from * @param string $topath Path we want to end up in * * @return string Path leading from $frompath to $topath */ function find_relative_path ( $frompath, $topath ) { $from = explode( DIRECTORY_SEPARATOR, $frompath ); // Folders/File $to = explode( DIRECTORY_SEPARATOR, $topath ); // Folders/File $relpath = ''; $i = 0; // Find how far the path is the same while ( isset($from[$i]) && isset($to[$i]) ) { if ( $from[$i] != $to[$i] ) break; $i++; } $j = count( $from ) - 1; // Add '..' until the path is the same while ( $i <= $j ) { if ( !empty($from[$j]) ) $relpath .= '..'.DIRECTORY_SEPARATOR; $j--; } // Go to folder from where it starts differing while ( isset($to[$i]) ) { if ( !empty($to[$i]) ) $relpath .= $to[$i].DIRECTORY_SEPARATOR; $i++; } // Strip last separator return substr($relpath, 0, -1); } var_dump(find_relative_path('D:/test/pokus/', 'test/file.less'));
Output for git.master, git.master_jit, rfc.property-hooks
string(23) "../../../test/file.less"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
46.73 ms | 401 KiB | 8 Q