如何从 PHP 中的用户输入中删除 URL 协议和斜杠

发布于 2024-10-06 06:07:59 字数 294 浏览 4 评论 0原文

用户输入示例

http://example.com/
http://example.com/topic/
http://example.com/topic/cars/
http://www.example.com/topic/questions/

我想要一个 PHP 函数来使输出如下

example.com
example.com/topic/
example.com/topic/cars/
www.example.com/topic/questions/

Example user input

http://example.com/
http://example.com/topic/
http://example.com/topic/cars/
http://www.example.com/topic/questions/

I want a PHP function to make the output like

example.com
example.com/topic/
example.com/topic/cars/
www.example.com/topic/questions/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

坠似风落 2024-10-13 06:07:59

ereg_replace 现已弃用,因此最好使用:

$url = preg_replace("(^https?://)", "", $url );

这会删除 http://https://

ereg_replace is now deprecated, so it is better to use:

$url = preg_replace("(^https?://)", "", $url );

This removes either http:// or https://

逆蝶 2024-10-13 06:07:59

您应该使用一系列“不允许的”术语并使用 strposstr_replace 从传入的 URL 中动态删除它们:

function remove_http($url) {
   $disallowed = array('http://', 'https://');
   foreach($disallowed as $d) {
      if(strpos($url, $d) === 0) {
         return str_replace($d, '', $url);
      }
   }
   return $url;
}

You should use an array of "disallowed" terms and use strpos and str_replace to dynamically remove them from the passed-in URL:

function remove_http($url) {
   $disallowed = array('http://', 'https://');
   foreach($disallowed as $d) {
      if(strpos($url, $d) === 0) {
         return str_replace($d, '', $url);
      }
   }
   return $url;
}
翻了热茶 2024-10-13 06:07:59

我建议使用 PHP 提供的工具,看看 parse_url

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

上面的示例将输出:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path

听起来您至少在​​ host + path (根据需要添加其他内容,例如 query):

$parsed = parse_url('http://www.domain.com/topic/questions/');

echo $parsed['host'], $parsed['path'];

    > www.domain.com/topic/questions/

干杯

I'd suggest using the tools PHP gave you, have a look at parse_url.

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

The above example will output:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path

It sounds like you're after at least host + path (add others as needed, e.g. query):

$parsed = parse_url('http://www.domain.com/topic/questions/');

echo $parsed['host'], $parsed['path'];

    > www.domain.com/topic/questions/

Cheers

风柔一江水 2024-10-13 06:07:59

创建一个数组:

$remove = array("http://","https://");

并替换为空字符串:

str_replace($remove,"",$url);

它看起来像这样:

function removeProtocol($url){
    $remove = array("http://","https://");
    return str_replace($remove,"",$url);
}

如果您的干草堆(输入)是字符串并且您用字符串替换数组中的针,则 Str_replace 将返回一个字符串。这很好,这样您就可以避免所有额外的循环。

Create an array:

$remove = array("http://","https://");

and replace with empty string:

str_replace($remove,"",$url);

it would look something like this:

function removeProtocol($url){
    $remove = array("http://","https://");
    return str_replace($remove,"",$url);
}

Str_replace will return a string if your haystack (input) is a string and you replace your needle(s) in the array with a string. It's nice so you can avoid all the extra looping.

国产ˉ祖宗 2024-10-13 06:07:59

哇。我从谷歌来到这里,希望找到一个可以复制和粘贴的衬垫!

您不需要函数来执行此操作,因为已经存在一个函数。只需执行以下操作:

echo explode("//", "https://anyurl.any.tld/any/directory/structure", 2)[1];

在本例中,explode() 将返回一个数组:

["https:", "anyurl.any.tld/any/directory/structure"]

我们想要第二个元素。这将处理 http、https、ftp 或几乎任何 URI,而不需要正则表达式。

https://www.php.net/manual/en/function.explode.php

如果您想要一个功能:

function removeProtocols($uri) { return explode("//", $uri, 2)[1]; }

编辑:请参阅 Harry Lewis 的用户评论...这是我现在最喜欢的方法。

Wow. I came here from google expecting to find a one liner to copy and paste!

You don't need a function to do this because one already exists. Just do:

echo explode("//", "https://anyurl.any.tld/any/directory/structure", 2)[1];

In this example, explode() will return an array of:

["https:", "anyurl.any.tld/any/directory/structure"]

And we want the 2nd element. This will handle http, https, ftp, or pretty much any URI, without needing regex.

https://www.php.net/manual/en/function.explode.php

If you want a function:

function removeProtocols($uri) { return explode("//", $uri, 2)[1]; }

EDIT: See user comment from Harry Lewis... this is my favourite way to do this now.

许久 2024-10-13 06:07:59

您可以使用带有 preg_replace 的正则表达式在一行中删除 https 和 http

fn (string $url): string
    => preg_replace('~^https?://~', '', $url);

You can remove both https and http in one line using a regular expression with preg_replace:

fn (string $url): string
    => preg_replace('~^https?://~', '', $url);
我最亲爱的 2024-10-13 06:07:59

您可以使用 PHP 的解析 url 功能。这适用于所有协议,甚至 ftp:// 或 https://

Eiter 获取协议组件并从 Url 中减去它,或者只是将其他部分连接在一起...

http://php.net/manual/de/function.parse-url.php

You could use the parse url Functionality of PHP. This will work for all Protocols, even ftp:// or https://

Eiter get the Protocol Component and substr it from the Url, or just concatenate the other Parts back together ...

http://php.net/manual/de/function.parse-url.php

小鸟爱天空丶 2024-10-13 06:07:59

这个解决方案对我有用,简短的一个。

parse_url($url, PHP_URL_HOST);

This solution works for me, short one.

parse_url($url, PHP_URL_HOST);
那支青花 2024-10-13 06:07:59
<?php
// user input
$url = 'http://www.example.com/category/website/wordpress/wordpress-security/';
$url0 = 'http://www.example.com/';
$url1 = 'http://www.example.com/category/';
$url2 = 'http://www.example.com/category/website/';
$url3 = 'http://www.example.com/category/website/wordpress/';

// print_r(parse_url($url));
// echo parse_url($url, PHP_URL_PATH);

$removeprotocols = array('http://', 'https://');

echo '<br>' . str_replace($removeprotocols,"",$url0);
echo '<br>' . str_replace($removeprotocols,"",$url1);
echo '<br>' . str_replace($removeprotocols,"",$url2);
echo '<br>' . str_replace($removeprotocols,"",$url3);

?>
<?php
// user input
$url = 'http://www.example.com/category/website/wordpress/wordpress-security/';
$url0 = 'http://www.example.com/';
$url1 = 'http://www.example.com/category/';
$url2 = 'http://www.example.com/category/website/';
$url3 = 'http://www.example.com/category/website/wordpress/';

// print_r(parse_url($url));
// echo parse_url($url, PHP_URL_PATH);

$removeprotocols = array('http://', 'https://');

echo '<br>' . str_replace($removeprotocols,"",$url0);
echo '<br>' . str_replace($removeprotocols,"",$url1);
echo '<br>' . str_replace($removeprotocols,"",$url2);
echo '<br>' . str_replace($removeprotocols,"",$url3);

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文