如何从 php 中的用户输入中删除 http、https 和斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4357668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:47:51  来源:igfitidea点击:

How do I remove http, https and slash from user input in php

phpfunctionpreg-replace

提问by Blur

Example user input

示例用户输入

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

I want a php function to make the output like

我想要一个 php 函数来使输出像

domain.com
domain.com/topic/
domain.com/topic/cars/
www.domain.com/topic/questions/

Let me know :)

让我知道 :)

采纳答案by Jacob Relkin

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

您应该使用一组“不允许的”术语并使用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;
}

回答by

ereg_replaceis now deprecated, so it is better to use:

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

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

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

这将删除http://https://

回答by Madbreaks

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

我建议使用 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);
?>

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):

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

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

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

    > www.domain.com/topic/questions/

Cheers

干杯

回答by Jordan Casey

Create an array:

创建一个数组:

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

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

and replace with empty string:

并替换为空字符串:

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

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.

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

Happy Coding!

快乐编码!

回答by minaz

You can remove both https and http in one line using ereg_replace:

您可以使用 ereg_replace 在一行中同时删除 https 和 http:

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

回答by Paul Weber

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

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

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

Eiter 获取协议组件并将其从 URL 中删除,或者只是将其他部分连接在一起......

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

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

回答by OpenWebWar

<?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);

?>

回答by mike-source

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")[1];

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

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

["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.

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

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

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

If you want a function:

如果你想要一个函数:

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

回答by Ives.me

Found this http://refactormycode.com/codes/598-remove-http-from-url-string

发现这个http://refactormycode.com/codes/598-remove-http-from-url-string

function remove_http($url = '')
{
    if ($url == 'http://' OR $url == 'https://')
    {
        return $url;
    }
    $matches = substr($url, 0, 7);
    if ($matches=='http://') 
    {
        $url = substr($url, 7);     
    }
    else
    {
        $matches = substr($url, 0, 8);
        if ($matches=='https://') 
        $url = substr($url, 8);
    }
    return $url;
}

回答by Iyad Al aqel

if its the first characters in the string you can use substr(0,8) , and it will remove the first 8th character if its not use the "str_replace()" function http://php.net/manual/en/function.str-replace.php

如果它是字符串中的第一个字符,您可以使用 substr(0,8) ,如果不使用“str_replace()”函数,它将删除第一个第 8 个字符 http://php.net/manual/en/function .str-replace.php