Add http:// prefix to URL when missing

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

Hello I have a very simple code


    
Website
10条回答
  •  粉色の甜心
    2020-11-30 07:18

    I think you'd better use the built in function parse_url() which returns an associative array with its components

    something like this will work for you:

     if  ( $ret = parse_url($url) ) {
    
          if ( !isset($ret["scheme"]) )
           {
           $url = "http://{$url}";
           }
    }
    

提交回复
热议问题