8

Can someone tell me how to strip anything that appears between http:// and /? For example, http://something/ or http://something.something.something/ so it changes to just /?

7 Answers 7

12

Assuming you're working with a URL and not a long string containing a url...

$path = parse_url($url, PHP_URL_PATH);
Sign up to request clarification or add additional context in comments.

1 Comment

This will not give the assumed expected result if the url has a query string and a fragment.
9

You could use parse_url but then you would have to re-build the rest of the URL with different components:

$components = parse_url($url);
$result = $components['path'] . '?' .
          $components['query'] . '#' .
          $components['fragment'];

1 Comment

This is definately the best answer, tiny update I made: $components = parse_url($url); $result = $components['path'] . (!empty($components['query']) ? '?' . $components['query'] : '') . (!empty($components['fragment']) ? '#' . $components['fragment'] : '');
2

The following will simply return the Path of your URL:

<?php

$urls = array(
    'http://something.com/',
    'http://something.something.com/',
    'http://something.something.com/some/path',
    'http://something.something.com/some/path/?query=string',
    'http://something.something.com/some/path/?query=string#with-fragment',
);

foreach ($urls as $url) {
    var_dump(parse_url($url, PHP_URL_PATH));
}

#=> string(1) "/"
#=> string(1) "/"
#=> string(10) "/some/path"
#=> string(11) "/some/path/"
#=> string(11) "/some/path/"

Note, if you're just looking to strip the URL Scheme and the Host off the URL, and you want to keep the Query and the Fragment, you can use this:

foreach ($urls as $url) {
    $url = parse_url($url);
    $ret = $url['path'];
    if ($url['query']) $ret .= "?{$url[query]}";
    if ($url['fragment']) $ret .= "#{$url[fragment]}";
    var_dump($ret);
}

#=> string(1) "/"
#=> string(1) "/"
#=> string(10) "/some/path"
#=> string(24) "/some/path/?query=string"
#=> string(38) "/some/path/?query=string#with-fragment"

Better yet, If you're using the PECL HTTP Extension, you can use the http_build_url() method:

foreach ($urls as $url) {
    $url = http_build_url(array_key_intersect(
        parse_url($url),
        array_flip(array('path', 'query', 'fragment'))
    ));
    var_dump($url);
}

#=> string(1) "/"
#=> string(1) "/"
#=> string(10) "/some/path"
#=> string(24) "/some/path/?query=string"
#=> string(38) "/some/path/?query=string#with-fragment"

Comments

2
$url = "http://php.net/manual/en/function.parse-url.php?arg=value#anchor";

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

The above example will output:

Array
(
    [scheme] => http
    [host] => php.net
    [path] => /manual/en/function.parse-url.php
    [query] => arg=value
    [fragment] => anchor
)

please refer to parse-url

Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string (except when PHP_URL_PORT is given, in which case the return value will be an integer).

Comments

0

What you want appears to be the $_SERVER['REQUEST_URI']. Or you can get that with parse_url()

Or if you insist on using regex, [EDIT] don't.

2 Comments

removed my -1 after you removed the regex answer. thank you for encouraging use of parse_url().
@macek thanks for reminding me that regex even if possible should sometimes downright not be used!
0
$url = "www.domain.com/sds?test=1&test2=2&test3=3";
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $query_array);

Doesn't matter what before "?". Will output

array {
 'test' => string '1' (length=1)
 'test2' => string '2' (length=1)
 'test3' => string '3' (length=1)
}

Comments

0

You can get it from the first /, // not included.

$url = "https://www.facebook.com/hhhhhh?2342";
$pos = strpos($url,"://");
$pos = false === $pos ? 0 : $pos + 3;
echo substr($url,strpos($url,'/',$pos));

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.