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 552921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:33:03+00:00 2026-05-13T11:33:03+00:00

i need to test if any of the strings ‘hello’, ‘i am’, ‘dumb’ exist

  • 0

i need to test if any of the strings ‘hello’, ‘i am’, ‘dumb’ exist in the longer string called $ohreally, if even one of them exists my test is over, and i have the knowledge that neither of the others will occur if one of them has.

Under these conditions I am asking for your help on the most efficient way to write this search,

strpos() 3 times like this?

if (strpos ($ohreally, 'hello')){return false;}  
   else if (strpos ($ohreally, 'i am')){return false;}  
   else if (strpos ($ohreally, 'dumb')){return false;}  
   else {return true;}

or one preg_match?

if (preg_match('hello'||'i am'||'dumb', $ohreally)) {return false}   
   else {return true};

I know the preg_match code is wrong, i would really appreciate if someone could offer the correct version of it.

Thank You!


Answer

Please read what cletus said and the test middaparka did bellow. I also did a mirco time test, on various strings, long and short. with these results

IF, you know the probability of the string values occurring ORDER them from most probable to least. (I did not notice a presentable different in ordering the regex itself i.e. between /hello|i am|dumb/ or /i am|dumb|hello/.

On the other hand in sequential strpos the probability makes all the difference. For example if ‘hello’ happens 90%, ‘i am’ 7% and ‘dumb’ 3 percent of the time. you would like to organize your code to check for ‘hello’ first and exit the function as soon as possible.

my microtime tests show this.

for haystacks A, B, and C in which the needle is found respectively on the first, second, and third strpos() execution, the times are as follows,

strpos:
A: 0.00450 seconds // 1 strpos()
B: 0.00911 seconds // 2 strpos()
C: 0.00833 seconds // 3 strpos()
C: 0.01180 seconds // 4 strpos() added one extra

and for preg_match:
A: 0.01919 seconds // 1 preg_match()
B: 0.02252 seconds // 1 preg_match()
C: 0.01060 seconds // 1 preg_match()

as the numbers show, strpos is faster up to the 4rth execution, so i will be using it instead since i have only 3, sub-stings to check for : )

  • 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-13T11:33:04+00:00Added an answer on May 13, 2026 at 11:33 am

    The correct syntax is:

    preg_match('/hello|i am|dumb/', $ohreally);
    

    I doubt there’s much in it either way but it wouldn’t surprise me if the strpos() method is faster depending on the number of strings you’re searching for. The performance of strpos() will degrade as the number of search terms increases. The regex probably will to but not as fast.

    Obviously regular expressions are more powerful. For example if you wanted to match the word “dumb” but not “dumber” then that’s easily done with:

    preg_match('/\b(hello|i am|dumb)\b/', $ohreally);
    

    which is a lot harder to do with strpos().

    Note: technically \b is a zero-width word boundary. “Zero-width” means it doesn’t consume any part of the input string and word boundary means it matches the start of the string, the end of the string, a transition from word (digits, letters or underscore) characters to non-word characters or a transition from non-word to word characters. Very useful.

    Edit: it’s also worth noting that your usage of strpos() is incorrect (but lots of people make this same mistake). Namely:

    if (strpos ($ohreally, 'hello')) {
      ...
    }
    

    will not enter the condition block if the needle is at position 0 in the string. The correct usage is:

    if (strpos ($ohreally, 'hello') !== false) {
      ...
    }
    

    because of type juggling. Otherwise 0 is converted to false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a dynamic URL preferably from a Test Class. Can any
I need to test if a string (filenames with their complete path) contains another
I need to compare two strings, containing HTML text. The test should return true
I have a string like this ch:keyword ch:test ch:some_text I need a regular expression
I need to test Perl Application ( File operation , Data base operation..etc )
I need to test if a user can write to a folder before actually
I need to test my application. Its a VoIP but each time I need
I need to test the bandwidth I have on my USB RNDIS connection. I
I need a test for a variable which would evaluate to true in two
I need to test, if an instance is exactly of a given type. But

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.