Skip to main content
38 events
when toggle format what by license comment
Feb 17, 2022 at 19:12 comment added mpen @mbomb007 Try these: php.net/manual/en/function.str-starts-with.php#125913
Feb 16, 2022 at 18:42 comment added mbomb007 What about endsWith?
Apr 15, 2018 at 18:12 comment added Thanh Trung $haystack[0] will throw a notice error if you don't test it with isset. The same for needles. But if you add tests, it will slow down its performance
Dec 15, 2017 at 19:02 comment added mpen @SalmanA You raise some good points. Not sure I want to redesign the test cases right now though.
Dec 15, 2017 at 15:06 comment added Salman Arshad These tests are no good in testing the performance. What you are doing is using random string as the needle. In 99.99% cases there will be NO match. Most of the functions will exit after matching first byte. What about cases when a match is found? Which function takes least amount of time to conclude successful match? What about cases where 99% of needle match but not the last few bytes? Which function takes least amount of time to conclude no match?
Nov 21, 2017 at 2:15 comment added Visman @mpen, I noticed not the elephant at all :(
Nov 20, 2017 at 17:23 comment added mpen @Visman Yeah...but I asked about putting a literal \E in the search string, not just a start/end delim.
Nov 19, 2017 at 12:26 comment added Visman @mpen, sandbox.onlinephpfunctions.com/code/… - working, sandbox.onlinephpfunctions.com/code/… - does not work. Probably a bug. regex101.com/r/kFBxWR/1 - the emulator is working properly.
Nov 18, 2017 at 21:26 comment added mpen @Visman You'd still have to double the backslashes in the needle if you used \Q...\E no? Edit: Or I guess just \E you'd have to escape... actually I don't know how to escape a literal \E. Edit2: I guess you could str_replace('\\E', '\\E\\\\E\\Q', $needle) which is even uglier than preg_quote. Not sure if it'd be faster or not.
Nov 18, 2017 at 4:35 comment added Visman Use return preg_match('~\Q' . $needle . '\E~A', $haystack) > 0;
Nov 18, 2017 at 4:22 comment added Visman preg_quote() is a very slow function. with - preg_match_startswith: 2,240.1 ms; without - preg_match_startswith: 258.0 ms
Jun 28, 2017 at 19:46 history edited Артур Курицын CC BY-SA 3.0
added 275 characters in body
Sep 19, 2016 at 16:45 comment added mpen @AdamJones I didn't come up with any alternatives. Here's what I'm using now: gist.github.com/mnpenner/1e35f9f20d0982c541c6ea1fd45ff5a8
Sep 19, 2016 at 16:13 comment added AdamJones @mpen any chance of showing us your preferred endsWith() alternatives as well ?
Aug 23, 2016 at 18:24 history edited mpen CC BY-SA 3.0
made it more PHP sevenie
Aug 23, 2016 at 18:13 comment added mpen Updated for PHP 7. strncmp_startswith2 moved from 4th place to 1st. Most functions got significantly faster, except preg_match which is now twice as slow.
Aug 23, 2016 at 18:09 history edited mpen CC BY-SA 3.0
deleted 1110 characters in body
Apr 19, 2016 at 11:01 comment added rivimey Another repro, on a 2015 Macbook Pro (Core i5) with php 5.6.20. Loops = 20000: generating tests....................done! substr_startswith: 39.290904998779 ms preg_match_startswith: 1911.6959571838 ms substr_compare_startswith: 37.993907928467 ms strpos_startswith: 51.711082458496 ms strncmp_startswith: 42.64497756958 ms strncmp_startswith2: 42.857885360718 ms
Jul 28, 2015 at 14:35 comment added FrancescoMM This is what I got on the same set on OsX MAMP environment on 10000 loops, to confirm it depends on the setup. generating tests..........done! substr_startswith: 51.8469810486 ms preg_match_startswith: 1991.78195 ms substr_compare_startswith: 53.2069206238 ms strpos_startswith: 63.8959407806 ms strncmp_startswith: 45.2649593353 ms strncmp_startswith2: 42.7808761597 ms so.. do some testing on you production machine!
May 10, 2015 at 22:18 history edited mpen CC BY-SA 3.0
deleted 1 character in body
Dec 18, 2014 at 3:08 comment added Jronny Darn, I don't know what went to my head that time. Prolly the lack of sleep.
Dec 2, 2014 at 16:41 comment added mpen @Jronny Because 110 is less than 133...??
Dec 2, 2014 at 14:22 comment added Jronny why did you say that substr_startswith is the fastest when substr_compare_startswith only has 133?
Nov 19, 2014 at 7:46 comment added FrancescoMM strange, it may depend on the PHP implementation as I had tested it on your same set, anyway thanks for adding it
Nov 19, 2014 at 0:59 history edited mpen CC BY-SA 3.0
added 84 characters in body
Nov 19, 2014 at 0:49 comment added mpen @FrancescoMM I've incorporated your version into my tests. That does not appear to be true; at least not when the first letter has a 1 in 64 chance of being a match.
Nov 19, 2014 at 0:48 history edited mpen CC BY-SA 3.0
Updated with more clear tests/results
Nov 19, 2014 at 0:42 history edited mpen CC BY-SA 3.0
Updated with more clear tests/results
Aug 19, 2014 at 15:46 history edited mpen CC BY-SA 3.0
updated results for php 5.5.9
S Apr 20, 2014 at 1:17 history suggested rvighne CC BY-SA 3.0
Removed superfluous "revised on" date, since anyone can just see revision history.
Apr 20, 2014 at 1:13 review Suggested edits
S Apr 20, 2014 at 1:17
Jan 10, 2014 at 13:01 history edited Zuul CC BY-SA 3.0
deleted 1 characters in body
Jul 28, 2013 at 15:38 comment added FrancescoMM If the strings are not empty, as in your tests, this is actually somehow (20-30%) faster: function startswith5b($haystack, $needle) {return ($haystack{0}==$needle{0})?strncmp($haystack, $needle, strlen($needle)) === 0:FALSE;} I added a reply below.
Mar 1, 2013 at 0:58 history edited mpen CC BY-SA 3.0
deleted 5 characters in body
Mar 1, 2013 at 0:51 history edited mpen CC BY-SA 3.0
added 862 characters in body
Aug 26, 2011 at 15:18 history edited mpen CC BY-SA 3.0
added 21 characters in body
Aug 24, 2011 at 0:14 history edited mpen CC BY-SA 3.0
added 968 characters in body
Aug 24, 2011 at 0:07 history answered mpen CC BY-SA 3.0