Add http:// prefix to URL when missing

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

Hello I have a very simple code


    
Website
相关标签:
10条回答
  • 2020-11-30 06:56

    I personally use this, which is partially taken from php docs

    $scheme = parse_url($link, PHP_URL_SCHEME);
    if (empty($scheme)) {
        $link = 'http://' . ltrim($link, '/');
    }
    
    0 讨论(0)
  • 2020-11-30 06:57

    Here is another example with string subtraction:

    $changeLink = $myRow->site_url;
      if(substr($changeLink, 0, 7) != 'http://') {
         $changeLink = 'http://' . $changeLink;  
    }
    

    // ....

    echo "<a href=\"" . $changeLink . "\" target=\"_blank\"></a>";
    
    0 讨论(0)
  • 2020-11-30 06:58

    Something like this?

    if (!strpos($aProfileInfo['Website'], 'http://')) {
        $aProfileInfo['Website'] = 'http://' . $aProfileInfo['Website'];
    }
    
    0 讨论(0)
  • 2020-11-30 07:01

    There are two ways of tackling this problem: url parsing and regular expressions.

    Some will say url parsing is right, but regular expressions work just as well in this case. I like being able to have simple one-liners for things like this especially because this would be a common occurrence in template files where you may need a one-liner inside an echo statement to maintain readability.

    Regular Expressions

    We can do this in a single function call with preg_replace.

    preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website'])
    

    This uses a negative lookahead at the beginning of the string that looks for http:// or https://. If either are found, the replace doesn't happen. If they aren't found, it replaces the beginning of the string (0 characters) with http:// essentially prepending this to the string without modifying it.

    In context:

    <a href="'. preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website']).'" target="_self">
        <div class="callButton">Website</div>
    </a>
    

    URL Parsing

    (parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']
    

    What this does is find out if a scheme is present on the link throught parse_url($aProfileInfo['Website'], PHP_URL_SCHEME). Then using a ternary operator, it will either output '' if there was one found or 'http://' if one wasn't found. Then it appends the link onto that.

    In context:

    <a href="'.((parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']).'" target="_self">
        <div class="callButton">Website</div>
    </a>
    
    0 讨论(0)
  • 2020-11-30 07:05

    I believe David's answer is the proper way to do this, but it can be simplified like this:

    parse_url($aProfileInfo['Website'], PHP_URL_SCHEME)==''?'http://'.$aProfileInfo['Website']:$aProfileInfo['Website']
    
    0 讨论(0)
  • 2020-11-30 07:06

    You also may take into account that "http(s)" must be at the beginning of the url:

    if (preg_match('/^https?:\/\//', $aProfileInfo['Website']) === 0) {
        $aProfileInfo['Website'] = 'http://'.$aProfileInfo['Website'];
    }
    
    0 讨论(0)
提交回复
热议问题