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

Asked

Viewed 384 times

0

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

The moment php picks up this content it gives problem, the impression I have is that it thinks it is a variable.

Follow a hypothetical example:

In the archive ini:

[config]
pwd=123456!@#$%

In php file after reading the ini:

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

As the content of "pwd" la no ini has character $, I imagine he understands that it’s a variable, when in fact the $ is part of the string.

I tried to put in double quotes but it doesn’t work:

$email_pwd = "$pwd";

If I put the literal string with simple quotes it accepts:

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

How to get around this situation to keep the password in the file ini?

1 answer

1


http://php.net/manual/en/function.parse-ini-file.php#refsect1-Function.parse-ini-file-Notes

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 quotes.

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

Update

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

key="teste " aspas " duplas"

Exit:

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

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

Escaping the string:

key="\"aspas duplas\""

Running code: https://3v4l.org/1FAla

INI_SCANNER_RAW

Filing cabinet .ini without using exhaust

key=""aspas duplas""

Use the flag INI_SCANNER_RAW

parse_ini($string , false , INI_SCANNER_RAW);

Running code: https://3v4l.org/Z8YRb

For both cases, the output is the same:

array(1) {
  ["key"]=>
  string(14) ""aspas duplas""
}
  • I read the link and understood your example, but another question arose... and if in the password we have double quotes how to escape? , I would quote inside quotation marks? Ex. "test&'&", see that I have double and single quotes inside the string.

  • @Marcelo I updated the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.