且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP - 在URL中将http替换为https

更新时间:2023-02-23 10:00:55


我只是使用一个简单的 str_replace()

  $ url = str_replace(http,https,$ url,1); 

第四个参数1指示它只替换http一次,打破您的网址。





无法删除,因为它已被接受, href =http://***.com/a/5289289/285578> @Jakub Hampl's answer。


I am trying to figure out how to add s after HTTP once a user checks a box in the html form.

I have in my PHP,

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

if(!isset($_POST['https'])) { 
  //something here
}

So basically, when the user checks a box with the name="https" i want to add s to $url's http making it https://google.com.

I have little knowledge on PHP and if someone can explain to me how to go about doing this, this would be really helpful! thanks.

I'd just use a simple str_replace()

$url = str_replace("http", "https", $url, 1);

The 4th parameter "1" tells it to only replace "http" one time so it doesn't break your url.

Can't delete this because it's accepted but see @Jakub Hampl's answer.