Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php getting substring from a string

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

Thanks a lot.

like image 868
Alex Pliutau Avatar asked Dec 09 '25 22:12

Alex Pliutau


1 Answers

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

like image 135
Thomas O Avatar answered Dec 12 '25 11:12

Thomas O