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?!
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
$parsed_string=substr($original_string, 0, 6); if (stristr($parsed_string, "http://")) { echo "yes, it contains http://"; }
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?!
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.
I still think that parse_url would be ideal for this if(parse_url($url, PHP_URL_SCHEME) == 'http') { //Code here } PHP:
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: