Reputation: 16575
Well, i want remove slash and rest of url after domain name. in this code:
$url = $_POST['url'];
$result = preg_replace('#/[^/]*$#', '', $url);
echo $result;
it will remove slash and after it (/index.php), but only when URL be something like this:
but in this:
or more slashes it will only remove last slash and trail (/test).
I want remove first slash after domain:
Example:
Remove:
/index/test/test/test/test/test/test
Result:
I don't know how to define first slash after domain and trails. Second problem is when url is:
it will remove /test.com but i never want this, i want when url hasn't any slash after domain name it DO NOT remove second slash from http:// ! well, i know i should define that remove first slash after domain or in other hand first slash in path or php self.
Upvotes: 0
Views: 1873
Reputation: 91375
How about:
$result = preg_replace('#((?:https?://)?[^/]*)(?:/.*)?$#', '$1', $url);
This will kepp everything that is before the first slash (after http:// if present)
Upvotes: 3
Reputation: 72177
You are using the wrong tool. This is a job for function parse_url()
. Use it to parse the URL into components (scheme, user+pass, host+port, path, query string, fragment) then pick the pieces you need and put them together to get the URL you want.
$url = $_POST['url'];
// Parse the URL into pieces
$pieces = parse_url($url);
// Put some of the pieces back together to get a new URL
// Scheme ('http://' or 'https://')
$newUrl = $pieces['scheme'].'://';
// Username + password, if present
if (! empty($pieces['user'])) {
$newUrl .= $pieces['user'];
if (! empty($pieces['pass'])) {
$newUrl .= ':'.$pieces['pass'];
}
$newUrl .= '@';
}
// Hostname
$newUrl .= $pieces['host'];
// Port, if present
if (! empty($pieces['port'])) {
$newUrl .= ':'.$pieces['port'];
}
// That't all. Ignore path, query string and url fragment
echo($newUrl);
As a side note, the composition part could be easily done using function http_build_url(); unfortunately, this function is provided by the HTTP extension which is not bundled with PHP. It has to be installed separately, which means that if the code is not hosted on your own server, it is most probably not available.
Upvotes: 0
Reputation: 98871
$result = preg_replace('%((https?://)?.*?)/.*$%', '$1', $url);
Explanation:
((https?://)?.*?)/.*$
Match the regex below and capture its match into backreference number 1 «((https?://)?.*?)»
Match the regex below and capture its match into backreference number 2 «(https?://)?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character string “http” literally «http»
Match the character “s” literally «s?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character string “://” literally «://»
Match any single character that is NOT a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “/” literally «/»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Assert position at the end of the string, or before the line break at the end of the string, if any «$»
$1
Insert the text that was last matched by capturing group number 1 «$1»
Upvotes: 0