11

Possible Duplicate:
Check if the url is contains the http or https

What is the code to figure out whether the given URL contains http:// or https:// at the beginning using PHP and regular expressions?

2
  • strpos documentation, first example. Commented Sep 19, 2011 at 8:41
  • 4
    Yes Mr Tomalak - if I knew regular expressions I would solve it myself and really don't need this kind of comments here - I'm trying to find the answer rather then read pointless comments like this one. Commented Sep 19, 2011 at 8:58

3 Answers 3

39

you can use parse_url

<?php
   $url = parse_url('https://example.org');

   if ($url['scheme'] == 'https') {
       // is https;
   }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

simple and efficient !
14
if (substr($string, 0, 7) == "http://") {
   $res = "http";
}

if (substr($string, 0, 8) == "https://") {
   $res = "https";
}

Comments

-5

Maybe this could help

$_SERVER['SERVER_PROTOCOL'];

2 Comments

This returns "HTTP/1.1", nothing to do with https.
He asked how to get the protocol from an url, which might not be necessarily the current page, it could be a generic string and in that case the array $_SERVER would be completely useless (not to mention the key 'SERVER_PROTOCOL' is wrong because it doesn't tell if the connection is SSL). If the string to parse is also the page url then you could use $_SERVER["REQUEST_SCHEME"] which returns 'http' or 'https' or $_SERVER["HTTPS"] which returns 1 if the scheme is https.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.