views:

40

answers:

5

Hello,

I would like to know how can I subtract only a part of the full path:

I get the full path of the current folder:

$dbc_root = getcwd(); // That will return let's say "/home/USER/public_html/test2"

I want to select only "/public_html/test2"

How can I do it? Thanks!

A: 

Well, if you know what the part of the path you want to discard is, you could simply do a str_replace:

$dbc_root = str_replace('/home/USER/', '', $dbc_root);
Erik
+1  A: 

I think you should check the path related methods:

pathinfo() - Returns information about a file path
dirname() - Returns directory name component of path
basename() - Returns filename component of path

You should be able to find a solution with one of these.

andreas
This wont work if you have two or more directories above of the `USER` directory.
Jonathan Czitkovics
+1  A: 

Depends on how fixed the format is. In easiest form:

$dbc_root = str_replace('/home/USER', '', getcwd());

If you need to get everything after public_html:

preg_match('/public_html.*$/', getcwd(), $match);
$dbc_root = $match;
Tatu Ulmanen
Thanks this is it :)
Adrian
A: 

You can replace the /home/USER with an empty string:

$path=str_replace("/home/USER", "", getcwd());
Jonathan Czitkovics
A: 
<?php
function pieces($p, $offset, $length = null)
{
  if ($offset >= 0) $offset++; // to adjust for the leading /
  return implode('/', array_slice(explode('/', $p), $offset, $length));
}

echo pieces('/a/b/c/d', 0, 1); // 'a'
echo pieces('/a/b/c/d', 0, 2); // 'a/b'
echo pieces('/a/b/c/d', -2); // 'c/d'
echo pieces('/a/b/c/d', -2, 1); // 'c'
?>
konforce