23

I'm sure this is a simple solution, just haven't found exactly what I needed.

Using php, i have a variable $source. I wanna check if $source starts with 'http'.

if ($source starts with 'http') {
 $source = "<a href='$source'>$source</a>";
}

Thanks!

John Carter
  • 53,924
  • 26
  • 111
  • 144
Andelas
  • 2,022
  • 7
  • 36
  • 45

6 Answers6

52
if (strpos($source, 'http') === 0) {
    $source = "<a href=\"$source\">$source</a>";
}

Note I use ===, not == because strpos returns boolean false if the string does not contain the match. Zero is falsey in PHP, so a strict equality check is necessary to remove ambiguity.

Reference:

http://php.net/strpos

http://php.net/operators.comparison

Jonah
  • 9,991
  • 5
  • 45
  • 79
  • 3
    You confused the haystack with the needle. – Ben Dec 11 '10 at 23:48
  • Worked like a charm. Thanks so much for the quick reply :) And the extra links to read about strpos – Andelas Dec 12 '10 at 00:06
  • 1
    It is more visually clean than using `substr()`, but the inefficiency niggles at me - it'll be scanning the whole of `$source` in order to find the position of `'http'`. Of course 99% of the time this won't matter, but still... – John Carter Dec 12 '10 at 00:12
  • @therefromhere: You have an intriguing point. Although, it seems like it would stop searching as soon as it has a match, wouldn't it? I don't know... we'd have to run a performance test. – Jonah Dec 12 '10 at 00:14
  • 1
    Also note that you've actually introduced a bug that wasn't in the question sample code - you've got ' instead of ". Fixing this myself. – John Carter Dec 12 '10 at 00:14
  • @Jonah, yes it would stop as soon as it got a match, but for all the strings that *don't* start with 'http' it'd scan the whole string. Whereas if you used `substr` it'd only ever look at the letters we care about (ie the first 4). – John Carter Dec 12 '10 at 00:16
  • @therefromhere: There's no bug in that code. The whole string is encapsulated within single quotes, so the double quotes inside of it are treated as normal characters (i.e. they don't close the string). Try it yourself if you don't believe me. – AgentConundrum Dec 12 '10 at 00:20
  • 1
    Ahh, I get it now. I actually ran a test :) `strpos` took 0.0000003287 seconds, while `substr` took 0.0000003881. But that's with "http" at the beginning. – Jonah Dec 12 '10 at 00:27
  • 1
    Okay, now I'm really creeped out. Without "http" at the beginning, `strpos` took 0.0000003440 seconds, while `substr` took 0.0000004285 seconds. What's with this? Here's the test code if you want to try it yourself: http://pastebin.com/LdYBbaS3 – Jonah Dec 12 '10 at 00:30
  • 1
    @Agent - yes, there is a bug. The single quotes mean $source won't be replaced with the variable value, so you'll get literally '`$source`' in the string. – John Carter Dec 12 '10 at 00:32
  • @Jonah props for actually testing! That is surprising. My guess is that `strpos` is faster since it returns an int whereas `substr` needs to do some memory allocation to return the string. If I could be bothered I'd run this on valgrind to check, but it's late :) It would be interesting to see if the same result occurs on a more varied data set. – John Carter Dec 12 '10 at 00:39
  • @therefromhere: Yes, it surprised me too. What you said sounds right. The comparison is being run in PHP with `substr`, but it's being run natively with `strpos`. – Jonah Dec 12 '10 at 00:45
  • -1 - this would include strings with 'http' in the middle. The question asks how to check for strings that start with 'http'. – squarecandy Jan 30 '15 at 17:31
  • @squarecandy I'm fairly sure it will only match the beginning. `strpos` returns the position of the needle (`"http"`) within the haystack, and then the code above checks to make sure it's at position `0` (the beginning of the haystack). Perhaps there's a bug here that I've missed? Can you clarify? – Jonah Jan 30 '15 at 19:48
  • 1
    ok, nevermind - i guess I misunderstood how you were using it. Does work with ===0 – squarecandy Jan 30 '15 at 20:27
16

You want the substr() function.

if(substr($source, 0, 4) == "http") {
   $source = "<a href='$source'>$source</a>";
}
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
  • Oops, needs to be `0, 4`, not `4`. `4` would get everything _but_ the http :) – Jonah Dec 11 '10 at 23:48
  • @Jonah: I was editing that while you were writing the comment. ["Shit! I always do that. I always mess up some mundane detail." -- Michael Bolton](http://www.imdb.com/title/tt0151804/) – AgentConundrum Dec 11 '10 at 23:51
  • You should test it, I believe it will fetch the entire string *from* position 4 – Itay Moav -Malimovka Dec 11 '10 at 23:52
  • @Itay: I already fixed it. I just typed it so fast that I didn't notice I missed the second parameter. – AgentConundrum Dec 11 '10 at 23:53
  • @Agent: it's a rush here on StackOverflow. This place is very competitive; StackExchange did an amazing job of figuring out how to motivate people. – Jonah Dec 11 '10 at 23:56
  • @Jonah: I know. Today has been a busy day for me. I got 'Mortarboard' today (yay!), then decided to see just how high I can go. :) – AgentConundrum Dec 11 '10 at 23:58
  • @Agent: Cool, what is the daily cap? I might be almost there... 200 so far today. – Jonah Dec 12 '10 at 00:02
  • @Jonah: You get the badge for hitting 200 rep, but you get rep from upvotes until you get 200 from upvotes only. Even after that, bonuses like accepted answers are exempt. Since SO goes based on UTC, "yesterday" ended 11 minutes ago. I got 265 rep for the day, just under double my previous record of 140. – AgentConundrum Dec 12 '10 at 00:12
  • @Jonah: I just looked at the reputation tab for your account. If I added it up right, you should have got 210 rep "yesterday". Expect the mortarboard badge sometime within the next few hours, and congratulations. – AgentConundrum Dec 12 '10 at 00:16
7
if(strpos($source, 'http') === 0)
    //Do stuff
Ben
  • 16,275
  • 9
  • 45
  • 63
  • 1
    Probably the most elegant way to check if a string starts with some substring in PHP. – Robert Dec 11 '10 at 23:52
  • Lolwut? You really ask why and state that `strpos` (what I just commented to be the most elegant way) is more clear? – Robert Dec 11 '10 at 23:56
  • @Robert: Oh, hehe, I mis-read. I thought you were saying that the `substr` solution was better. My bad :) – Jonah Dec 11 '10 at 23:59
5

Use substr:

if (substr($source, 0, 4) === 'http')
casablanca
  • 69,683
  • 7
  • 133
  • 150
1
if(preg_match('/^(http)/', $source)){
...
}
ali
  • 41
  • 1
  • 4
  • 1
    It would be helpful to explain your solution. Specifically the preg_match function and regex. This will help others understand how they could possibly use these tools for other problems. – badmadrad Nov 14 '18 at 22:08
1

As of PHP 8.0 there is method str_starts_with implemented:

if (str_starts_with($source, 'http')) {
    $source = "<a href='$source'>$source</a>";
} 
Jsowa
  • 9,104
  • 5
  • 56
  • 60