3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Path { public $currentPath; private $array; function __construct( $path ) { $this->currentPath = $path; $this->array = array_slice( explode( "/", $path ), 1 ); } public function cd( $newPath ) { if ( $newPath == "" || $newPath == "." ) { return $this; } $change = explode( "/", $newPath ); foreach ( $change as $value ) { if ( $value == ".." ) { if ( count( $this->array ) > 0 ) { array_pop($this->array); } } elseif ( $value !== "" ) { array_push( $this->array, $value ); } } if ( count( $this->array ) > 0 ) { $this->currentPath = implode( "/", $this->array ); } else { $this->currentPath = "/"; } return $this; } } $path = new Path('/a/b/c/d'); echo $path->cd('../x')->currentPath;

preferences:
32.87 ms | 402 KiB | 5 Q