tags:

views:

71

answers:

4
+1  Q: 

find string match

I need to check a variable to see if the string starts with http:// or https://

if it exists do something else do another. How would I go about doing that?

+4  A: 
preg_match('!^https?://!', $string)

docs

SilentGhost
wow that worked great. I didn't realize preg_match gives a boolean if it is true or false. Do you have a doc page for the regular expression? Looking at it now I think I get it...^ is begin with....? is the previous character could be an s...don't know what the ! mean.
Keith
`preg_match()` doesn't return a boolean. It returns the number of matches it found; as it stops after the first match, it can just return 0, or 1. If you are checking the return value as in `if (preg_match() == TRUE) {}`, then you will not get the expected result.
kiamlaluno
Down-voted for regex. Although it's technically correct, it's bad advice. See Gordons answer for a much faster solution.
The Pixel Developer
Gordon's answer is wrong.
SilentGhost
'much faster', seriously? Besides Gordon's answer simply checks if the string begins with http, that's not the same thing as checking if it begins with http:// or https://
Cags
@The Pixel Developer: I wish people would stop down-voting regex just because it’s a regex.
Gumbo
The !(exlcamation) is a delimiter. These are use in PCRE Regex to separate the pattern from modifiers.. ie /strings?/i would match strings, Strings, STRINGS, stringS, etc.
Zane Edward Dockery
@The Pixel Developer: Show us proof with benchmarks and how significant that a single regex conditional test is slower than two strpos() tests.
stillstanding
This is getting ridiculous now, but my solution (two strpos) is indeed more than twice as fast as the regex (0.000002 vs 0.000005 s avg own time). I am not claiming it is significant nor did I downvote the regex solution, but since you are so keen on *technical* details, you have to acknowledge that the Regex *is* much slower - even if it is insignificant. But then again, you dont know how many strings the OP wants to test this way, do you? So who knows, maybe it is significant.
Gordon
@Gordon: while I'm not rejecting your numbers, I'd make a wild guess that you didn't test non-matching strings.
SilentGhost
@SilentGhost even a a string of 1000 a's will still be faster with double strpos, though you are right that the time taken gets closer to the regex solution then. And like I said, it's getting ridiculous now :)
Gordon
Sounds like I started a gang fight in here. Thank you all for the answers. I learned a lot. Technically isn't strpos correct because lets say I have a string: $string = "hellohttps://there";Wouldn't the regex say it exists even though it didn't check if it was the first character?
Keith
@Keith the `^` makes sure the Regex matches only if the string starts with http. Both, Regex and `strpos` are valid approaches. The PHP manual suggests to avoid Regex for simple matching, because the string functions are often (not always) faster. If that speed difference is significant is totally dependent on your UseCase. Usually, it is neglectable and a micro-optimization.
Gordon
+3  A: 

The question is unclear. If you just want to know if the string starts with either http or https, you can use

$startsWithIt = (strpos($haystack, 'http') === 0);

If you want the check to include :// you have to do:

if((strpos($haystack, 'http://') === 0) || 
   (strpos($haystack, 'https://') === 0)) {
       echo 'Strings starts with http:// or https://';    
}

If you need to know which of the two it is, use Wrikken's answer.

Gordon
A sensible answer. I wish people would stop up-voting regex for trivial string matching.
The Pixel Developer
@Pixel: this answer doesn't solve the problem.
SilentGhost
This does only check if the string starts with `http` <del>or `https`</del> but not if it starts with `http://` or `https://`.
Gumbo
@Gumbo @SilentGhost no, it checks for `http` only. I am pragmatically assuming that the OP just wants to know if there is any URL Scheme at all. Like I said, the question is vague about the actual UseCase.
Gordon
or starts with httpanythingelse, so technically it doesn't meet the requirements. Sure, you could modify it to be (strpos($haystack, 'http://')===0||strpos($haystack,'https://')===0) but that doesn't change the validity of the statement by SilentGhost.
Cags
+6  A: 

You're probably looking for:

switch(parse_url($string, PHP_URL_SCHEME)){
    case 'https':
        //do something
    case 'http':
        //something else
    default:
       //anything else you'd like to support?
}
Wrikken
`$urlScheme = parse_url($string, PHP_URL_SCHEME); switch($urlScheme) {` would be even more fantastic, but this is still an awesome answer.
erisco
+1  A: 

Here is another one:

function startsWithHttpOrHttps($haystack)
{
    return substr_compare($haystack, 'http://', 0, 7) === 0
        || substr_compare($haystack, 'https://', 0, 8) === 0;
}

On my machine, this outperformed my double strpos solution (even with added substr to prevent going over the full length string) and Regex.

Gordon