Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Quick help

  1. #1
    New Coder
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quick help

    I need a code snippit to do this



    if("http://" is at the begging of the variable $url)
    {
    do nothing
    }
    else
    {
    append to the beginning of the variable $url
    }

  • #2
    Regular Coder
    Join Date
    May 2003
    Location
    34 54' N 82 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $url 
    "http://www.codingforums.com";
    if(
    preg_match(/http/, $url)){
    echo 
    ""/*Does Nothing*/
    }
    else
    {
    /*What do you mean by - "append  to the beginning of the variable $url"*/
    }
    ?>
    What do you mean by - "append to the beginning of the variable $url"??

    Hope this helps.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #3
    New Coder
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm I'm not sure if that would work

    I want it to check to see if "http://" (<-- the whole thing) is at the beginning of $url and if not, add it

    so if $url = http://www.site.com, it's left alone but
    if $url = www.anothersite.com, it's changed to http://www.anothersite.com

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could use regex, but i think a simple substr will be more effincient

    so
    PHP Code:
    if (substr($url,0,7) != "http://"){
        
    $newurl "http://" ;
    }
    $newurl .= $url 
    should do the trick

  • #5
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    or

    Code:
    preg_match("/http:\/\//i",$url);

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Better check for

    Code:
    if ( !preg_match("/^http:\/\//i",$url) ) { // etc.
    since it's only important to know if "http" is at the beginning of the string, not somewhere else.
    De gustibus non est disputandum.

  • #7
    New Coder
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by raf
    you could use regex, but i think a simple substr will be more effincient

    so
    PHP Code:
    if (substr($url,0,7) != "http://"){
        
    $newurl "http://" ;
    }
    $newurl .= $url 
    should do the trick
    That one worked and I modified it just a little bit, just changed it not to use the $newurl variable


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •