note 76453 added to function.realpath
| From: | julabz at laposte dot net | Date: | Mon, 16 Jul 2007 18:49:03 +0000 |
| Subject: | note 76453 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128629@lists.php.net to get a copy of this message | ||
Here's a little function to return the relative path between two urls:
<?php
function get_relative_path($start_dir, $final_dir){
//
$firstPathParts = explode(DIRECTORY_SEPARATOR, $start_dir);
$secondPathParts = explode(DIRECTORY_SEPARATOR, $final_dir);
//
$sameCounter = 0;
for($i = 0; $i < min( count($firstPathParts), count($secondPathParts) ); $i++) {
if( strtolower($firstPathParts[$i]) !== strtolower($secondPathParts[$i]) ) {
break;
}
$sameCounter++;
}
if( $sameCounter == 0 ) {
return $final_dir;
}
//
$newPath = '';
for($i = $sameCounter; $i < count($firstPathParts); $i++) {
if( $i > $sameCounter ) {
$newPath .= DIRECTORY_SEPARATOR;
}
$newPath .= "..";
}
if( count($newPath) == 0 ) {
$newPath = ".";
}
for($i = $sameCounter; $i < count($secondPathParts); $i++) {
$newPath .= DIRECTORY_SEPARATOR;
$newPath .= $secondPathParts[$i];
}
//
return $newPath;
}
?>
----
Server IP: 195.221.21.36
Probable Submitter: 86.66.130.168
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- https://master.php.net/note/edit/76453
Del: integrated -- https://master.php.net/note/delete/76453/integrated
Del: useless -- https://master.php.net/note/delete/76453/useless
Del: bad code -- https://master.php.net/note/delete/76453/bad+code
Del: spam -- https://master.php.net/note/delete/76453/spam
Del: non-english -- https://master.php.net/note/delete/76453/non-english
Del: in docs -- https://master.php.net/note/delete/76453/in+docs
Del: other reasons-- https://master.php.net/note/delete/76453
Reject -- https://master.php.net/note/reject/76453
Search -- https://master.php.net/manage/user-notes.php