Pretty simple but I cant get the exact syntax working.
I just want a true or false check to see if a string beings with ‘for the’ (case insensitive).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If it’s just that, then you could use plain text searching:
Or,
The comments below got me wondering about which is actually faster, so I wrote some benchmarking.
Here’s the TLDR version:
strposis really fast if you’re not working with large strings.strncmpis reliable and fast.preg_matchis never a good option.Here’s the long version:
return strpos($haystack, $needle) === 0return preg_match("/^$needle/", $haystack) === 1return substr($haystack, 0, strlen($needle)) === $needlereturn strncmp($needle, $haystack, strlen($needle)) === 0Interesting points:
strposon the long, entirely non-matching needle against the short haystack.strposrecorded the top 11 times.strposhad the best performance, it was weighed down by the long non-matching needles on the long haystack. They were 5-10 times slower than most tests.strncmpwas fast and the most consistent.preg_matchwas consistently about 2 times slower than the other functions