A script I am working on creates a string populated with 3-letter abbreviations. Another script searches the string for each of the abbreviations to check if certain conditions apply to each record in the database. I used the code

if(strpos(abbreviationsString,'XXX'))

to check the string for each abbreviation.
This worked, except one small problem. If the small string is found at the very first position of the big string, strpos returns ZERO. Thus, the if statement would interpret that as false.
I tried a few different things to resolve this, and ended up putting a 'dummy' character at the beginning of the big string so this would never happen.
I would like to know what others think of my solution and if NE1 has any better solutions.
Thanks in advance for your suggestions,
ffej2ffej

    if(strpos($string, 'XXX') !== false) {
       // it found it
    }
    

    Note the use of "!==", not "!=", so that it checks specifically for Boolean false and does not auto-cast 0 to false.

      Write a Reply...