views:

66

answers:

7

I have the next URL: http://domen.com/aaa/bbb/ccc. How can I get the string after http://domen.com/?

Thanks a lot.

+2  A: 

You can use PHP's split.

Your code will be something like:

$s = "http://domen.com/aaa/bbb/ccc";
$vals = split("http://domen.com/", $s);
// $v will contain aaa/bbb/ccc
$v = $vals[1];
Pablo Santa Cruz
PHP doesn't support array dereferencing, so `split(...)[0]` won't work. You'd need to store the result in a variable and then access the member...
ircmaxell
@ircmxaell: yup. thanks.
Pablo Santa Cruz
+10  A: 
$sub = substr($string, 0, 10);

But if you actually want to parse the URL (that is, you want it to work with all URLs), use parse_url. For "http://domen.com/aaa/bbb/ccc", it would give you an array like this:

Array
(
    [scheme] => http
    [host] => domen.com
    [user] => 
    [pass] => 
    [path] => /aaa/bbb/ccc
    [query] => 
    [fragment] => 
)

You could then compile this into the original url (to get http://domen.com/):

$output = $url['scheme'] . "://" . $url['host'] . $url['path'];

assuming $url contains the parse_url results.

Thomas O
+1 for `parse_url()`
BoltClock
The `substr` example would be more appropriate to this question if it were of the `substr($string, 17)` variety. But regardless, `parse_url` should be used if applicable.
konforce
^ I didn't count the characters.
Thomas O
+2  A: 

parse_url()

Mark Baker
+1  A: 

http://php.net/manual/en/function.parse-url.php Might be the way to go.

J Pollock
+1  A: 

If you simply want the string and the "http://domen.com/" part is fixed:

$url = 'http://domen.com/aaa/bbb/ccc';
$str = str_replace('http://domen.com/','',$url);
Alec
A: 

preg_replace('/^.*?\w\//', '', $url) ? :)

Harry
+1  A: 

Use the regex for example like the function preg_replace