Well, i want remove slash and rest of url after domain name. in this code:
好吧,我想在域名后删除斜杠和其余的url。在这段代码中:
$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:
它会删除斜杠和它之后(/index.php),但只有当URL是这样的时候:
but in this:
但在这:
or more slashes it will only remove last slash and trail (/test).
或更多斜杠它只会删除最后一个斜杠和轨迹(/ 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:
我不知道如何在域和路径后定义第一个斜杠。第二个问题是url是:
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.
它将删除/test.com但我从来不想要这个,我想当url在域名之后没有任何斜杠它不会从http://删除第二个斜杠!好吧,我知道我应该定义删除域之后的第一个斜杠或者在路径或php self中的第一个斜杠。
3 个解决方案
#1
How about:
$result = preg_replace('#((?:https?://)?[^/]*)(?:/.*)?$#', '$1', $url);
This will kepp everything that is before the first slash (after http:// if present)
这将锁定第一个斜杠之前的所有内容(在http://之后)
#2
$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»
#3
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.
您使用的是错误的工具。这是函数parse_url()的工作。使用它将URL解析为组件(方案,用户+传递,主机+端口,路径,查询字符串,片段),然后选择您需要的部分并将它们放在一起以获取所需的URL。
$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.
作为旁注,可以使用函数http_build_url()轻松完成组合部分;遗憾的是,此功能由HTTP扩展提供,该扩展未与PHP捆绑在一起。它必须单独安装,这意味着如果代码不在您自己的服务器上托管,则很可能无法使用。
#1
How about:
$result = preg_replace('#((?:https?://)?[^/]*)(?:/.*)?$#', '$1', $url);
This will kepp everything that is before the first slash (after http:// if present)
这将锁定第一个斜杠之前的所有内容(在http://之后)
#2
$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»
#3
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.
您使用的是错误的工具。这是函数parse_url()的工作。使用它将URL解析为组件(方案,用户+传递,主机+端口,路径,查询字符串,片段),然后选择您需要的部分并将它们放在一起以获取所需的URL。
$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.
作为旁注,可以使用函数http_build_url()轻松完成组合部分;遗憾的是,此功能由HTTP扩展提供,该扩展未与PHP捆绑在一起。它必须单独安装,这意味着如果代码不在您自己的服务器上托管,则很可能无法使用。