Search code examples
phpnewlineinistrpos

strpos: finding a value in .ini file


I have some code that looks like the following:

if (strpos($stringt, $key . '=') === false) {
    ...some code...
}

and is supposed to check if a particular key=value pair exists in an .ini file. The .ini file is just a PHP .ini file full of KEY=String values, one per line.

The problem is that if a shorter key happens to be present within a longer key, the line will return true instead of false. For example, if a key/value PROBLEM=problem exists and you check using: $key = 'EN', the routine will return true because $strpos will match 'EN=' within the longer string 'HAPPEN='. How can I get strpos to match a key only from the start of a new line (I tried '\n' . $key . '=' and that didn't work)?


Solution

  • if (strpos($stringt, "\n" . $key . '=') === false) {
        ...some code...
    }
    

    You had to use "\n" instead of '\n'