Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 541979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:23:57+00:00 2026-05-13T10:23:57+00:00

Pretty simple but I cant get the exact syntax working. I just want a

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:23:57+00:00Added an answer on May 13, 2026 at 10:23 am

    If it’s just that, then you could use plain text searching:

    if (stripos("for the", $text) === 0) { // case-insensitive here
        // string starts with "for the"
    }
    

    Or,

    if (substr($text, 0, 7) == "for the")
    

    The comments below got me wondering about which is actually faster, so I wrote some benchmarking.

    Here’s the TLDR version:

    • strpos is really fast if you’re not working with large strings.
    • strncmp is reliable and fast.
    • preg_match is never a good option.

    Here’s the long version:

    • Two different “haystacks”:
      1. 10000 characters of lipsum
      2. 83 characters of lipsum.
    • 5 different searching methods:
      1. strpos:
        return strpos($haystack, $needle) === 0
      2. preg_match
        return preg_match("/^$needle/", $haystack) === 1
      3. substr
        return substr($haystack, 0, strlen($needle)) === $needle
      4. strncmp
        return strncmp($needle, $haystack, strlen($needle)) === 0
      5. Manual looping:
    for ($i = 0, $l = strlen($needle); $i < $l; ++$i) {
        if ($needle{$i} !== $haystack{$i}) return false;
    }
    return true;
    • 7 different “needles”
      • 3 matching (lengths: 83, 5 and 1 character)
      • 4 non-matching (lengths: 83, 82, 5 and 1 characters). The 82 char needle doesn’t match at all, and the 83 character needles matches all but the last character.
    • 100,000 iterations, per method per needle per haystack

    Interesting points:

    • The fastest individual test of all was strpos on the long, entirely non-matching needle against the short haystack.
      • In fact, out of the 14 tests run per method, strpos recorded the top 11 times.
    • The slowest individual test was the manual method on the long needles, regardless of haystack size. Those four tests were 10-20 times slower than almost all the other tests.
    • Though strpos had 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.
    • strncmp was fast and the most consistent.
    • preg_match was consistently about 2 times slower than the other functions
    Haystack: 83 characters
                  ______________________________________________________________
     ____________|__________ non-matching ___________|_______  matching ________|
    | function   |   1    |   5    |   82   |   83   |   1    |   5    |   83   |
    |------------+--------+--------+--------+--------+--------+--------+--------|
    | manual     | 0.2291 | 0.2222 | 0.2266 | 4.1523 | 0.2337 | 0.4263 | 4.1972 |
    | preg_match | 0.3622 | 0.3792 | 0.4098 | 0.4656 | 0.3642 | 0.3694 | 0.4658 |
    | strncmp    | 0.1860 | 0.1918 | 0.1881 | 0.1981 | 0.1841 | 0.1857 | 0.1980 |
    | strpos     | 0.1596 | 0.1633 | 0.1537 | 0.1560 | 0.1571 | 0.1589 | 0.1681 |
    | substr     | 0.2052 | 0.2066 | 0.2009 | 0.2166 | 0.2061 | 0.2017 | 0.2236 |
    -----------------------------------------------------------------------------
    
    Haystack: 10000 characters
                  ______________________________________________________________ 
     ____________|__________ non-matching ___________|_______  matching ________|
    | function   |   1    |   5    |   82   |   83   |   1    |   5    |   83   |
    |------------+--------+--------+--------+--------+--------+--------+--------|
    | manual     | 0.2275 | 0.2249 | 0.2278 | 4.1507 | 0.2315 | 0.4233 | 4.1834 |
    | preg_match | 0.3597 | 0.3628 | 0.4147 | 0.4654 | 0.3662 | 0.3679 | 0.4684 |
    | strncmp    | 0.1886 | 0.1914 | 0.1835 | 0.2014 | 0.1851 | 0.1854 | 0.1989 |
    | strpos     | 0.1605 | 2.1877 | 2.3737 | 0.5933 | 0.1575 | 0.1597 | 0.1667 |
    | substr     | 0.2073 | 0.2085 | 0.2017 | 0.2152 | 0.2036 | 0.2090 | 0.2183 |
    -----------------------------------------------------------------------------
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty straightforward, but I just want to know which is faster. I think simply
Seems pretty simple but I can't get it to work. I have two divs
I'd like to build a pretty simple plug-in for Visual Studio, but I don't
I'm piecing together an image website. The basic schema's pretty simple MySQL, but I'm
I'm pretty sure this is a simple question in regards to formatting but here's
I am pretty new to php, but I am learning! I have a simple
Should be pretty simple: I have an InputStream where I want to peek at
This is pretty simple, I come from a swing/awt background. I'm just wondering what
Pretty simple scenario. I have a web service that receives a byte array that
Pretty simple question: When i have a persistable object, it usually has a property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.