Add http:// prefix to URL when missing

前端 未结 10 1213
谎友^
谎友^ 2020-11-30 06:41

Hello I have a very simple code


    
Website
10条回答
  •  情歌与酒
    2020-11-30 07:12

    You can use this function as a general if nothing from the array is found in the string append something to it.

    function httpify($link, $append = 'http://', $allowed = array('http://', 'https://')){
    
      $found = false;
      foreach($allowed as $protocol)
        if(strpos($link, $protocol) !== 0)
          $found = true;
    
      if($found)
        return $link;
      return $append.$link;
    }
    

提交回复
热议问题