PHP-preg_替换删除斜杠和后斜杠

PHP-preg_替换删除斜杠和后斜杠,php,regex,apache,preg-replace,Php,Regex,Apache,Preg Replace,嗯,我想删除斜杠和域名后的url的其余部分。在此代码中: $url = $_POST['url']; $result = preg_replace('#/[^/]*$#', '', $url); echo $result; 它将删除斜杠及其后(/index.php),但仅当URL类似于以下内容时: 但在这方面: 一个或多个斜杠它只会删除最后一个斜杠和轨迹(/test) 我要删除域后的第一个斜杠: 示例: 删除: /索引/测试/测试/测试/测试/测试/测试 结果: 我不知道如何定义域和路

嗯,我想删除斜杠和域名后的url的其余部分。在此代码中:

$url = $_POST['url'];
$result = preg_replace('#/[^/]*$#', '', $url);
echo $result;
它将删除斜杠及其后(/index.php),但仅当URL类似于以下内容时:

但在这方面:

一个或多个斜杠它只会删除最后一个斜杠和轨迹(/test)

我要删除域后的第一个斜杠:

示例:

删除:

/索引/测试/测试/测试/测试/测试/测试

结果:

我不知道如何定义域和路径之后的第一个斜杠。 第二个问题是当url为:

它将删除/test.com,但我永远不想这样做,我想当url在域名后没有任何斜杠时,不要删除http://中的第二个斜杠!好的,我知道我应该定义在域后删除第一个斜杠,或者在路径或php self中删除第一个斜杠。

怎么样:

$result = preg_replace('#((?:https?://)?[^/]*)(?:/.*)?$#', '$1', $url);
这将删除第一个斜杠之前的所有内容(在http://if-present之后)

如何:

$result = preg_replace('#((?:https?://)?[^/]*)(?:/.*)?$#', '$1', $url);
这将删除第一个斜杠之前的所有内容(在http://if-present之后)

如何:

$result = preg_replace('#((?:https?://)?[^/]*)(?:/.*)?$#', '$1', $url);
这将删除第一个斜杠之前的所有内容(在http://if-present之后)

如何:

$result = preg_replace('#((?:https?://)?[^/]*)(?:/.*)?$#', '$1', $url);
这将kepp第一个斜杠之前的所有内容(在http://if-present之后)


说明:

((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»

说明:

((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»

说明:

((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»

说明:

((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»

你用错了工具。这是一项功能性工作。使用它将URL解析为组件(scheme、user+pass、host+port、path、查询字符串、fragment),然后选择所需的片段并将它们组合在一起以获得所需的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);


作为旁注,使用函数可以很容易地完成组成部分;不幸的是,此函数是由提供的提供的。它必须单独安装,这意味着如果代码不是托管在您自己的服务器上,它很可能不可用。

您使用了错误的工具。这是一项功能性工作。使用它将URL解析为组件(scheme、user+pass、host+port、path、查询字符串、fragment),然后选择所需的片段并将它们组合在一起以获得所需的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);


作为旁注,使用函数可以很容易地完成组成部分;不幸的是,此函数是由提供的提供的。它必须单独安装,这意味着如果代码不是托管在您自己的服务器上,它很可能不可用。

您使用了错误的工具。这是一项功能性工作。使用它将URL解析为组件(scheme、user+pass、host+port、path、查询字符串、fragment),然后选择所需的片段并将它们组合在一起以获得所需的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);


作为旁注,使用函数可以很容易地完成组成部分;不幸的是,此函数是由提供的提供的。它必须单独安装,这意味着如果代码不是托管在您自己的服务器上,它很可能不可用。

您使用了错误的工具。这是一项功能性工作。使用它将URL解析为组件(scheme、user+pass、host+port、path、查询字符串、fragment),然后选择所需的片段并将它们组合在一起以获得所需的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);



作为旁注,使用函数可以很容易地完成组成部分;不幸的是,此函数是由提供的提供的。它必须单独安装,这意味着如果代码不是托管在您自己的服务器上,它很可能不可用。

与prev-answer类似,它工作正常,但当url为
http://site.domain
它将删除
http://
中的第二个斜杠,就像前面的回答一样,它可以正常工作,但当url为
http://site.domain
它将删除
http://
中的第二个斜杠,就像上一个答案一样,它工作正常,但当url为
http://site.domain
它将删除
http://
中的第二个斜杠,就像前面的回答一样,它可以正常工作,但当url为
http://site.domain
它将删除http://中的第二个斜杠,非常好,但测试后发现错误。如果url是
test.domain
(当用户在没有http://的情况下输入url时)
test.domain
不是url:-)如果需要获取url,那么无论使用何种方法生成,都必须检查输入字符串是否包含方案和主机名(至少)。非常好,但测试后发现错误。如果url是
test.domain
(当用户在没有http://的情况下输入url时)
test.domain
不是url:-)如果需要获取url,那么无论使用何种方法生成,都必须检查输入字符串是否包含方案和主机名(至少)。非常好,但测试后发现错误。如果url是
test.domain
(当用户在没有http://的情况下输入url时)
test.domain
不是url:-)如果需要获取url,那么无论使用何种方法生成,都必须检查输入字符串是否包含方案和主机名(至少)。非常好,但测试后发现错误。如果url是
test.domain
(当用户在没有http:///的情况下输入url时)
test.domain
不是url:-)如果需要获取url,那么无论使用何种方法生成,都必须检查输入字符串是否包含方案和主机名(至少)。很好!一个简单的编辑可以使用:
$result=preg_replace('#((?:http(.*{0,1}://)?[^/]*)(?:/.?)$#','$1',$url)-以这种方式,它包括https URL。域名有其验证regexp,因此此答案不正确。很好!一个简单的编辑可以使用:
$result=preg_replace('#((?:http(.*{0,1}://)?[^/]*)(?:/.?)$#','$1',$url)-以这种方式,它包括https URL。域名有其验证regexp,因此此答案不正确。很好!一个简单的编辑可以使用:
$result=preg_replace('#((?:http(.*{0,1}://)?[^/]*)(?:/.?)$#','$1',$url)-以这种方式,它包括https URL.domain na