2

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

Thanks a lot.

7 Answers 7

10
$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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
^ I didn't count the characters.
2

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];

1 Comment

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...
2

parse_url()

Comments

1

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

Comments

1

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);

Comments

1

Use the regex for example like the function preg_replace

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.