How to add "http" to the beginning of bad links?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    How to add "http" to the beginning of bad links?

    Hi,

    I have a scalar variable, "$url" that may or not contain "http://" in
    front of it. If it is not there, I'd like to add it. Unfortunately,
    this code isn't working for me with PHP 4.

    if (strpos(strtolo wer($url), "http://") != 0) {
    $url = "http://$url";
    } // if

    How can I correct?

    Thanks, - Dave

  • NC

    #2
    Re: How to add "http&quot ; to the beginning of bad links?

    laredotornado@z ipmail.com wrote:[color=blue]
    >
    > I have a scalar variable, "$url" that may or not contain "http://"
    > in front of it. If it is not there, I'd like to add it. Unfortunately,
    > this code isn't working for me with PHP 4.
    >
    > if (strpos(strtolo wer($url), "http://") != 0) {
    > $url = "http://$url";
    > } // if
    >
    > How can I correct?[/color]

    if (strpos(strtolo wer($url), "http://") !== false) {
    $url = "http://$url";
    }

    Read the Manual, it's all there:

    Find the position of the first occurrence of a substring in a string


    Cheers,
    NC

    Comment

    • Rik

      #3
      Re: How to add "http&quot ; to the beginning of bad links?

      NC wrote:[color=blue]
      > laredotornado@z ipmail.com wrote:[color=green]
      >>
      >> I have a scalar variable, "$url" that may or not contain "http://"
      >> in front of it. If it is not there, I'd like to add it.
      >> Unfortunately, this code isn't working for me with PHP 4.
      >>
      >> if (strpos(strtolo wer($url), "http://") !=
      >> 0) { $url = "http://$url";
      >> } // if
      >>
      >> How can I correct?[/color]
      >
      > if (strpos(strtolo wer($url), "http://") !== false) {
      > $url = "http://$url";
      > }
      >[/color]

      It's just a pity for relative links, other protocols (https://, ftp://, even
      mailto:, all extremely valid). Then again, that wasn't the op's question...

      I'd:
      if(preg_match(' "^(https?://|ftp://|mailto:|\.{0,2 }/|smb://|file://|s?news:|l
      dap(i|s)?:|goph er:|nntp:|telne t:|mms:|ssh:)"s i', $url)<1) $url =
      'http://'.$url;

      I'm still forgetting a lot of other possibilities though...

      Grtz,
      --
      Rik Wasmus


      Comment

      • John Dunlop

        #4
        Re: How to add &quot;http&quot ; to the beginning of bad links?

        Rik:
        [color=blue]
        > if(preg_match(' "^(https?://|ftp://|mailto:|\.{0,2 }/|smb://|file://|s?news:|l
        > dap(i|s)?:|goph er:|nntp:|telne t:|mms:|ssh:)"s i', $url)<1) $url =
        > 'http://'.$url;
        >
        > I'm still forgetting a lot of other possibilities though...[/color]

        Forget about particular schemes and match scheme names (plus colon)
        in general:

        `^[a-z][a-z\d+.-]+:`i

        http://www.ietf.org/rfc/rfc3986.txt :3.1

        --
        Jock

        Comment

        Working...