Forum Moderators: coopster

Message Too Old, No Replies

how can i remove the file name from file path

         

shankimout

6:39 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



hi my friends , i have one question

how can i remove the file name from the file path?

for example :

[php.net...]

to

[php.net...]

whoisgregg

7:26 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I might explode [php.net] on "/", array_pop [php.net] the last array element, then implode [php.net] with a "/"

But I bet a regex guru can do it in a single line. :)

shankimout

8:28 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



YES , but when use the explode and exclude the last item , these inputs doesnt work

[php.net...]
[php.net...]

this will print [php.net...]

i want a code that print [php.net...] from these inputs
1 - [php.net...]
2 - [php.net...]
3 - [php.net...]

whoisgregg

8:35 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about these other possible scenarios?

4 - [php.net...]
// Not all filenames end with an extension

5 - [php.net...]
// Modrewrite wrecks havoc on knowing what's the file and what's a parameter

*https added to break the links, not particularly relevant to the topic.

shankimout

8:41 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



true , i dont need these but its better to use these 5 to write a regexp

can you write an regexp for this?

whoisgregg

8:57 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am *not* a regex guru. Actually, I haven't even achieved "regex town idiot" status yet, so I'm not the person to ask.

Regarding scenario #4, you'll have to choose whether or not the lack of a trailing forward slash makes the last bit a filename or a directory.

coopster

10:56 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PHP actually has quite a few nice functions for this without having to use regular expressions. dirname() is one of my favorites.
$path = 'http://php.net/downloads/index.php'; 
print dirname [php.net]($path) . "<br />";
print_r(pathinfo [php.net]($path));

shankimout

5:15 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



try this code .

function get_dir_name($dir){

$main_url = $dir;
$url_explode = explode("/", $main_url);
$file_name = end($url_explode);
$file_explode = explode(".", $file_name);
$quest_explode = explode("?", $file_name);
if(count($file_explode) > 1){$end_is_file = $file_explode[1];}else{$end_is_file = "";}
if(count($quest_explode) > 1){$end_is_quest = $quest_explode[1];}else{$end_is_quest = "";}
if ($end_is_file!= ""){
$result_url = str_replace($file_name, "", $main_url);
return $result_url;
}elseif ($end_is_quest!= ""){
$result_url = str_replace($file_name, "", $main_url);
return $result_url;
}else{
if(substr($main_url,-1,1)!= "/"){
$main_url .= "/";
}
return $main_url;
}
}