How to handle reserved character in an .ini file in php?

0

I have a .ini file where I store some information outside of the directory tree of the site, it happens that in a password I have special characters.

At the moment that php gets this content it gives problem, the impression I have is that it thinks it is a variable.

Here's a hypothetical example:

In the file ini :

[config]
pwd=123456!@#$%

In the php file after reading ini :

...
leu o ini
$pwd = $Arquivo["config"]["pwd"];
$email_pwd = $pwd;

As the content of "pwd" la in ini has $ character, I imagine it to be a variable, when $ is actually part of string .

I tried to enclose double quotation marks but it does not work:

$email_pwd = "$pwd";

If I put the literal string with single quotation marks it will accept:

$email_pwd = '123456!@#$%';

How to circumvent this situation to keep the password in the file ini ?

    
asked by anonymous 01.08.2018 / 21:26

1 answer

1

p>
  

If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes (").

You need to add the value inside double quotation marks.

[config]
pwd = "123456!@#$%"

Update

The file .ini can have apostrophes inside a string value, only it ends up ignoring them:

key="teste " aspas " duplas"

Output:

array(1) {
  ["key"]=>
  string(18) "teste aspas duplas"
}

To interpret the quotes, it can be done in two ways.

Escaping the string:

key="\"aspas duplas\""

Running code: link

INI_SCANNER_RAW

File .ini without using escape

key=""aspas duplas""

Use the INI_SCANNER_RAW flag

parse_ini($string , false , INI_SCANNER_RAW);

Running code: link

For both cases, the output is the same:

array(1) {
  ["key"]=>
  string(14) ""aspas duplas""
}
    
01.08.2018 / 21:37