Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

url

php

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

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
like image 495
JohnUS Avatar asked Dec 02 '22 23:12

JohnUS


2 Answers

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);
like image 154
inhan Avatar answered Dec 18 '22 14:12

inhan


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.

See also: 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;
...
like image 43
Captain Insaneo Avatar answered Dec 18 '22 14:12

Captain Insaneo