Check if variable starts with 'http'
37,154
Solution 1
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/operators.comparison
Solution 2
You want the substr()
function.
if(substr($source, 0, 4) == "http") {
$source = "<a href='$source'>$source</a>";
}
Solution 3
if(strpos($source, 'http') === 0)
//Do stuff
Solution 4
Use substr
:
if (substr($source, 0, 4) === 'http')
Solution 5
As of PHP 8.0 there is method str_starts_with
implemented:
if (str_starts_with($source, 'http')) {
$source = "<a href='$source'>$source</a>";
}
Related videos on Youtube
Author by
Robin
Updated on July 09, 2022Comments
-
Robin almost 2 years
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!
-
Jsowa over 3 yearsPHP 8.0 solution - stackoverflow.com/a/64160271/7082164
-
-
Ben over 13 yearsYou confused the haystack with the needle.
-
Jonah over 13 yearsOops, needs to be
0, 4
, not4
.4
would get everything but the http :) -
Mikeon over 13 years@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
-
Itay Moav -Malimovka over 13 yearsYou should test it, I believe it will fetch the entire string from position 4
-
Robert over 13 yearsProbably the most elegant way to check if a string starts with some substring in PHP.
-
Mikeon over 13 years@Itay: I already fixed it. I just typed it so fast that I didn't notice I missed the second parameter.
-
Jonah over 13 years@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.
-
Robert over 13 yearsLolwut? You really ask why and state that
strpos
(what I just commented to be the most elegant way) is more clear? -
Mikeon over 13 years@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. :)
-
Jonah over 13 years@Robert: Oh, hehe, I mis-read. I thought you were saying that the
substr
solution was better. My bad :) -
Jonah over 13 years@Agent: Cool, what is the daily cap? I might be almost there... 200 so far today.
-
Robin over 13 yearsWorked like a charm. Thanks so much for the quick reply :) And the extra links to read about strpos
-
Mikeon over 13 years@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.
-
John Carter over 13 yearsIt 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... -
Jonah over 13 years@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.
-
John Carter over 13 yearsAlso note that you've actually introduced a bug that wasn't in the question sample code - you've got ' instead of ". Fixing this myself.
-
Mikeon over 13 years@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.
-
John Carter over 13 years@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). -
Mikeon over 13 years@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.
-
Jonah over 13 yearsAhh, I get it now. I actually ran a test :)
strpos
took 0.0000003287 seconds, whilesubstr
took 0.0000003881. But that's with "http" at the beginning. -
Jonah over 13 yearsOkay, now I'm really creeped out. Without "http" at the beginning,
strpos
took 0.0000003440 seconds, whilesubstr
took 0.0000004285 seconds. What's with this? Here's the test code if you want to try it yourself: pastebin.com/LdYBbaS3 -
John Carter over 13 years@Agent - yes, there is a bug. The single quotes mean $source won't be replaced with the variable value, so you'll get literally '
<a href="$source">$source</a>
' in the string. -
John Carter over 13 years@Jonah props for actually testing! That is surprising. My guess is that
strpos
is faster since it returns an int whereassubstr
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. -
Jonah over 13 years@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 withstrpos
. -
squarecandy over 9 years-1 - this would include strings with 'http' in the middle. The question asks how to check for strings that start with 'http'.
-
Jonah over 9 years@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 position0
(the beginning of the haystack). Perhaps there's a bug here that I've missed? Can you clarify? -
squarecandy over 9 yearsok, nevermind - i guess I misunderstood how you were using it. Does work with ===0
-
badmadrad over 5 yearsIt 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.