php - strpos: finding a value in .ini file

486

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

893

Answer

Solution:

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

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

711

Answer

Solution:

I would just use PHP's built-in INI parser:

http://php.net/manual/en/function.parse-ini-file.php

print_r(parse_ini_file('yourinihere.ini'));

People are also looking for solutions to the problem: php - How do I redirect to referring page/url after successful login?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.