A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP get rid of slashes full path - page 1
User InfoPosts
PHP get rid of slashes full path#1
I have a full path which I would like to remove certain levels of it. So for instance,

/home/john/smith/web/test/testing/nothing/


I would like to get rid of 4 levels, so I get

/test/testing/nothing/


What would be a good of doing this?

Thanks

posted date: 2008-12-15 08:45:00


Re: PHP get rid of slashes full path#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-15 08:45:01


Re: PHP get rid of slashes full path#3
A simple solution is to slice the path up into parts, and then manipulate the array before sticking it back together again:join("/", array_slice(explode("/", $path), 5));Of course, if you wanted to remove that specific path, you could also use a regular expression:preg_replace('~^/home/john/smith/web/~', '', $path);One word of advice though. If your application is juggling around with paths a lot, it may be a good idea to create a class to represent paths, so as to encapsulate the logic, rather than have a lot of string manipulations all over the place. This is especially a good idea, if you mix absolute and relative paths.

posted date: 2008-12-15 08:49:00


Re: PHP get rid of slashes full path#4
Why would you use a needless regular expression? str_replace will do this fine, there is no pattern matching at all!

posted date: 2008-12-16 04:46:00


Re: PHP get rid of slashes full path#5
Why are you all using regular expressions for something that requires absolutely no matching; CPU cycles are valuable!str_replace would be more efficient:$s_path = '/home/john/smith/web/test/testing/nothing/';$s_path = str_replace('john/smith/web/test/', '', $s_path);And use realpath() to resolve any '../../' paths.And remember dirname(__FILE__) gets the CWD and rtrim() is extremely useful for removing trailing slashes..

posted date: 2008-12-16 04:49:00


Re: PHP get rid of slashes full path#6
Because it's rooted. str_replace will match anywhere in the string, while a regexp which starts with ^ will match only if the string begins with the pattern.

posted date: 2008-12-16 04:56:00


Re: PHP get rid of slashes full path#7
Your solution(s) only works for some subset of valid input. Besides, what makes you think str_replace performs better than a rooted regexp? Did you actually measure the difference? If you did, you would find that the difference is diminutive

posted date: 2008-12-16 05:09:00


Re: PHP get rid of slashes full path#8
Maybe once sure, but in a loop it is cumulative.

posted date: 2008-12-16 05:12:00


Re: PHP get rid of slashes full path#9
Who said he wanted to do that, he wanted to remove parts of the string? Quit giving negative votes simply due to the fact you disagree and aren't willing to step down from your pedestal.

posted date: 2008-12-16 05:13:00


Re: PHP get rid of slashes full path#10
Still not important. preg is very efficient. Try measuring it - You'd be surprised.

posted date: 2008-12-16 14:05:00


Re: PHP get rid of slashes full path#11
Quit being childish - It's not pretty.

posted date: 2008-12-16 14:06:00


Re: PHP get rid of slashes full path#12
Instead of using / it may be best to use the more portable PATH_SEPARATOR constant

posted date: 2008-12-20 22:07:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.