PHP - 用URL中的https替换http

Kev*_*ung 17 html php checkbox

一旦用户检查html表单中的框,我试图找出如何在HTTP之后添加s.

我有我的PHP,

$url = 'http://google.com';

if(!isset($_POST['https'])) { 
  //something here
}
Run Code Online (Sandbox Code Playgroud)

所以基本上,当用户选中名称为"https"的复选框时,我想将s添加到$ url的http,使其成为https://google.com.

我对PHP知之甚少,如果有人能向我解释如何去做,这将非常有用!谢谢.

Jak*_*mpl 62

$url = preg_replace("/^http:/i", "https:", $url);
Run Code Online (Sandbox Code Playgroud)


tiv*_*net 11

$url = str_replace( 'http://', 'https://', $url );
Run Code Online (Sandbox Code Playgroud)


Fel*_*ing 2

单程:

$url = '%s//google.com';
$protocol = 'http:';

if(!isset($_POST['https'])) { 
    $protocol = 'https:';
}

$url = sprintf($url, $protocol);
Run Code Online (Sandbox Code Playgroud)