0

I have a function which is used to add an http:// to a url,which do not have a http:// like as below

function addhttp($url) {
 if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
      $url = "http://" . $url;
  }

return $url;
}

My problem is ,

If i pass a url with &,the string after the & will skipped, eg:
https://www.example.com/Welcome/Default.aspx?scenarioID=360&pid=3308&treeid=1000 Returns

https://www.example.com/Welcome/Default.aspx?scenarioID=360

I lose &pid=3308&treeid=1000 this part,How to fix this error??

Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • I returns correctly, but how and where are you passing the url? – Robin Carlo Catacutan Apr 27 '15 at 06:45
  • This is actually a codeigniter function, and this will be like $url =$this->addhttp($_GET['u']); – Shijin TR Apr 27 '15 at 06:47
  • hmm ok, but I'm really not losing anything when I run your code. http://sandbox.onlinephpfunctions.com/code/67eb0e9fe39041db0ebb51ea975daa7fa424f818 – Robin Carlo Catacutan Apr 27 '15 at 06:59
  • I realize that $_GET['u'] returns only https://www.example.com/Welcome/Default.aspx?scenarioID=360 .that was the problem – Shijin TR Apr 27 '15 at 07:00
  • yes, since after the `&` is another pair. Maybe you should get the query after the `?` – Robin Carlo Catacutan Apr 27 '15 at 07:06
  • @RobinCarloCatacutan $extra_params = $this->input->get(); unset($extra_params['u']); if(!empty($extra_params)){ foreach($extra_params as $key => $data) { $url.='&'.$key.'='.$data; } } This code fix my error,but is this a good logic? – Shijin TR Apr 27 '15 at 07:07
  • It cost you another lines of code. If you can get the current url using code igniter, you can then use `parse_url` when getting the query. You can check it here http://php.net/manual/en/function.parse-url.php – Robin Carlo Catacutan Apr 27 '15 at 07:12

1 Answers1

2

I am unable to reproduce the error using PHP 5.5. However, personally I don't like to use a regular expression when there are built in functions that do the job. The following should work just fine as a replacement for the regular expression ~^(?:f|ht)tps?://~i:

<?php
function addhttp($url, $https=false) {
    $protocols = ['https://', 'http://', 'ftps://', 'ftp://'];
    $heystack = strtolower(substr($url, 0, 8));
    foreach ($protocols as $protocol) {
        if (strpos($heystack, $protocol) === 0) {
            return $url;
        }
    }
    return ($https ? 'https://' : 'http://') . $url;
}

$url = 'www.example.com/Welcome/Default.aspx?scenarioID=360&pid=3308&treeid=1000';
// for http://
echo addhttp($url); 
// for https://
echo addhttp($url, true); 

I added an optional parameter here, if you don't like it just take it out and remove the ternary expression (<expression> ? true : false).

If you need to get the value of the URL see this question

Community
  • 1
  • 1
robbmj
  • 16,085
  • 8
  • 38
  • 63
  • 1
    Another builtin function would be `parse_url('google.be', PHP_URL_SCHEME) !== null` – DarkBee Apr 27 '15 at 06:59
  • Yeah, good call @DarkBee. But it looks like there is a change in behaviour to the function post PHP 5.4.7 specific to the scheme portion of the URL being absent or not. check out the second example on the manual page: http://php.net/manual/en/function.parse-url.php – robbmj Apr 27 '15 at 07:01