如何从 URL 中删除 https:// 并在 PHP 的字符串中插入 http:// 而不是它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9486974/
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-26 06:56:09  来源:igfitidea点击:

How to remove https:// from URL and insert http:// instead of it in string in PHP?

phpurl

提问by JohnUS

First, I need to check the URL string, if the protocol of URL is https, then I need to replace httpin PHP. So the inputs and outputs of this php function must be like this:

首先,我需要检查 URL 字符串,如果 URL 的协议是https,那么我需要http在 PHP 中替换。所以这个php函数的输入输出一定是这样的:

Input -> https://example.com/example/https.php
Output-> http://example.com/example/https.php

Input -> http://example.com/example/https.php
Output-> http://example.com/example/https.php

回答by inhan

This will ensure it's at the beginning of the string and it's followed by ://

这将确保它位于字符串的开头,然后是 ://

$input = 'https://example.com/example/https.php';
echo preg_replace('/^https(?=:\/\/)/i','http',$input);

回答by Captain Insaneo

function remove_ssl ($url) {
    if (strpos($url, 'https://') == 0) {
        $url = 'http://' . substr($url, 7);
    }
    return $url;
}

The

strpos($url, 'https://') == 0

Is on purpose and is not === because we only want the case when the URL starts with https:// and just replace that one.

是故意的,而不是 ===,因为我们只想要 URL 以 https:// 开头的情况,然后替换那个。

See also: http://php.net/manual/en/function.parse-url.php

另见:http: //php.net/manual/en/function.parse-url.php

...
$parsed_url = parse_url($url);
if ($parsed_url['scheme'] == 'https') {
    $url = 'http://' . substr($url, 7);
}
return $url;
...

回答by Vyktor

At first you need to check httpspresence with strpos():

首先,您需要使用以下命令检查https是否存在strpos()

if( strpos( $url, 'https://') === 0){

(notice ===), than you may extract the all string after https://(that's after first 8 characters, or 5 when keeping original ://) with substr():

(注意===),然后您可以使用以下方法提取所有字符串https://(在前 8 个字符之后,或在保留原始字符时为 5 个字符之后://substr()

$url = 'http://' . substr( $url, 8);

回答by Onkar Janwa

  $parse = parse_url($url);
  if($parse['scheme'] === 'https')
   {
  $url = str_replace('https','http',$url,1);
}   

You can use this solution.

您可以使用此解决方案。

回答by Paul T.

function replace_uri_protocol($uri, $search, $replacement){
    $parts = parse_url($uri);
    $uri = $replacement."://".$parts["host"].$parts["path"];

    if(isset($parts["query"])){
        $uri .= "?".$parts["query"];
    }       

    return $uri;
}

回答by Brian Glaz

You can use a combination of str_pos()and str_replace()to accomplish this:

您可以使用的组合str_pos(),并str_replace()做到这一点:

if(str_pos($input,'https:') === 0) {
    $output = str_replace('https:','http:',$input,1);
} else {
    $output = $input;
}

回答by MAHABUB HOSSAIN RISHAD

Try this it's work on me..

试试这个它对我有用..

$url = 'https://www.example.com';
 echo preg_replace("(^https?://)", "http://", $url);