5

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
1
  • nice example. quite rare here.. Commented Feb 28, 2012 at 17:43

7 Answers 7

9

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);
Sign up to request clarification or add additional context in comments.

6 Comments

I think regular expressions might be overkill for this situation.
Major +1 for this. Prevents problems with all the other answers thus far where there might be more than one https in a string.
@JustinSatyr I agree, this is right place for using strncmp or strpos (which is faster than strncmp in php when string begins with needle).
@ceejayoz Why do you prefer RegEx instead of Vyktor's answer ?
@JohnUS Because a regex allows you to use things like ^ to only affect the beginning of a string, which is a useful feature in this case. Any speed differences are going to be pretty negligible if you're not a huge site like Facebook.
|
4
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;
...

4 Comments

You may have https:// multiple times in URL, it would be probably incorrect (some not URL-escaped entry) but it may happen: https://..../index.php?source=https://....
Hh, on your update, what about URLs like $url = 'http://username:password@hostname/path?arg=value#anchor';? :)) (but I like your way of thinking, +1)
Well, updated again...I believe that should work. Checks if it starts with https and only should only replace the first occurrence.
You've end up with my answer .)) so now I have nothing bad to say :) maybe that strncmp and/or strpos would be faster than parse_url, but this is a proper way to check protocol.
2

At first you need to check https presence with 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():

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

Comments

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

You can use this solution.

Comments

1

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

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

3 Comments

I think you want === 0 instead to be sure it is at the beginning of the string.
There can be "https:" in a string more than once.
@JustinSatyr good call. Updated answer to only replace the first instance of 'https:' and only if found at the beginning. (Notice the 4th count parameter added to str_replace.
1
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;
}

Comments

0

Try this it's work on me..

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.