Skip to main content
19 events
when toggle format what by license comment
Dec 19, 2017 at 11:34 comment added Mike Shiyan 3rd arguments to all str*pos() functions are unnecessary. They default to "0".
Oct 12, 2016 at 10:21 review Suggested edits
Oct 12, 2016 at 12:15
Jan 29, 2016 at 12:15 comment added nawfal This returns false for startsWith('abc', '') You got to check for empty string first.
Oct 14, 2015 at 18:50 comment added Félix Adriyel Gagnon-Grenier this. I really wonder why this is not the accepted startsWith() function.
Aug 4, 2015 at 15:13 history edited Ram Sharma CC BY-SA 3.0
Formatting
Apr 12, 2014 at 19:11 history edited Peter Mortensen CC BY-SA 3.0
Relative references to other answers are not reliable (change with view type ("active"/"oldest"/"votes"), later voting and possibly later answers) - but it would be even better to enumerate the answers that are alluded to. Made indentation consistent.
Nov 22, 2013 at 18:17 comment added Fabrício Matté return strpos($haystack, $needle, $expectedPosition) === $expectedPosition; should be better in that it doesn't scan the whole string when failing. Though then you will need an if ($expectedPosition < 0) return false; check in case the needle's length is greater than the haystack's.
Apr 2, 2013 at 13:14 comment added Sylvain Please don't use this example, you risk falling onto large performance problems.
Dec 3, 2012 at 3:47 comment added quietmint You should strongly consider quoting the needle like strpos($haystack, "$needle", 0) if there's any chance it's not already a string (e.g., if it's coming from json_decode()). Otherwise, the [odd] default behavior of strpos() may cause unexpected results: "If needle is not a string, it is converted to an integer and applied as the ordinal value of a character."
Aug 6, 2012 at 1:55 comment added mpen @wdev: Not a fair comparison. Two of your 3 tests have the needle start at the beginning, which is where strpos excels at. Run it just for "no match" and strncmp wins. I don't know why strpos would ever win though. Perhaps because strncmp is doing a full comparison, and not just equal/not-equal. And btw, I did do some benches if you had looked further down this thread; but regardless, I'm entitled to speculate.
Aug 6, 2012 at 1:05 comment added wdev @Mark Match, no match and same string comparison: gist.github.com/3268655
Aug 6, 2012 at 0:44 comment added mpen @wdev: did your string always contain the needle? strpos would be slowest when the string doesn't even contain the needle (has to scan the whole thing). and what are you comparing against exactly? using substr, or other methods? substr has to allocate a new string, there's a substantial cost in that too.
Aug 6, 2012 at 0:39 comment added wdev @mark I did some benchmarks with 1000 char haystack and 10 or 800 char needle and strpos was 30% faster. Do your benchmarks before stating that something is faster or not...
Sep 26, 2011 at 15:39 comment added chacham15 @Mark yea, checking just the beginning is a LOT faster, especially if you're doing something like checking MIME types (or any other place where the string is bound to be large)
Mar 3, 2011 at 21:26 comment added mpen you think scanning the entire string is cheaper than checking just the beginning? i don't think strpos is any cheaper.
Jan 4, 2011 at 15:46 comment added AppleGrew The strlen() thing is not unnecessary. In case the string doesn't start with the given needle then ur code will unnecessarily scan the whole haystack.
Aug 6, 2010 at 7:31 history edited Sander Rijken CC BY-SA 2.5
corrected off by 1 error
Aug 5, 2010 at 17:16 comment added Enrico Detoma endsWith() function has an error. Its first line should be (without the -1): $expectedPosition = strlen($haystack) - strlen($needle);
May 13, 2009 at 21:23 history answered Sander Rijken CC BY-SA 2.5