String question

Discussion in 'PHP' started by redhits, Nov 9, 2009.

  1. #1
    I have a string and i would like to know if the first 7 letters are http://, how i can do that ? without string[0]='h' ,string[t] ?!

    is there anything like string[0 to 6] ,etc?! for when i want to remove something from a string,etc?!
     
    redhits, Nov 9, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to use the first X letters take a look at substr()
    If this is to find out what the scheme of a URL is take a look at parse_url() with the PHP_URL_SCHEME flag
     
    JAY6390, Nov 9, 2009 IP
  3. Hosting24

    Hosting24 Member

    Messages:
    431
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    35
    #3
    $parsed_string=substr($original_string, 0, 6);
    if (stristr($parsed_string, "http://"))
    {
    echo "yes, it contains http://";
    }
     
    Hosting24, Nov 9, 2009 IP
  4. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #4
    I was also thinking to sub the sub string but it won't return an error if the string will be empty , or will have less characters then the number of characters i a searching for?!
     
    redhits, Nov 9, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    use the empty() function to check if the string is empty?
     
    JAY6390, Nov 9, 2009 IP
  6. jamshed88

    jamshed88 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Use substr function to get first few characters you can use it like this

    
    substr($str,0,7)
    
    PHP:
     
    jamshed88, Nov 9, 2009 IP
  7. vetrivel

    vetrivel Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    what wil happen if it https?


     
    vetrivel, Nov 9, 2009 IP
  8. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Why even think about https. OP wants http.

    With substr, the string is not removed but part of the string is assigned to a new variable.
     
    ads2help, Nov 9, 2009 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I still think that parse_url would be ideal for this
    if(parse_url($url, PHP_URL_SCHEME) == 'http') {
        //Code here
    }
    PHP:
     
    JAY6390, Nov 9, 2009 IP
  10. Hosting24

    Hosting24 Member

    Messages:
    431
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    35
    #10
    Easy to solve even with basic PHP skills...
     
    Hosting24, Nov 10, 2009 IP
  11. kbluhm

    kbluhm Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Well that is just redundant to double check with stristr().
    
    if ( 'http://' == substr( strtolower( $url ), 0, 7 ) )
    {
        // starts with `http://`
    }
    
    PHP:
    Or use stripos():
    
    if ( 0 === stripos( $url, 'http://' ) )
    {
        // starts with `http://`
    }
    
    PHP:
     
    Last edited: Nov 10, 2009
    kbluhm, Nov 10, 2009 IP