Edit report at https://bugs.php.net/bug.php?id=55091&edit=1
ID: 55091
Updated by: cmb@php.net
Reported by: PinoyHarryPotter at gmail dot com
Summary: parse_ini_file: last escaped double-quote of
multiline ends read
-Status: Open
+Status: Verified
-Type: Bug
+Type: Documentation Problem
Package: Filesystem function related
Operating System: Windows XP, CENTOS
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
> At first, I tried escaping new lines with \n.
You can, and sometimes must, escape newlines with a backslash, see
<https://3v4l.org/H6dY7m>. That is, however, not
documented.
Previous Comments:
------------------------------------------------------------------------
[2011-06-30 14:51:44] PinoyHarryPotter at gmail dot com
Description:
------------
When parsing an ini file with new lines and that the ini file requires double quotes to be escaped,
PHP considers the last double-quote character in a line as the closing quote regardless if you
escape the double-quote.
The story. I was trying to create an ini file to store the my Google AdSense. I was thinking that it
might be a violation in their TOS if I try to remove the new lines, so I leave it at is.
At first, I tried escaping new lines with \n. But that does not work. It prints out the actual \n.
From the PHP documentation, there's a comment that it should be \\n. Which I did and prints out
\n. So I did a research. Someone said to use \\\n which obviously does not work.
So I gave up trying so I use the actual line feed instead of using \n (I've read that this
might be problematic especially if for instance, you code it in Windows and there's carriage
return and then you ported it to Mac which gives out error when it finds it).
Now, the new line does work. Unfortunately, the moment it sees that the last character of the line
is a double-quote, PHP reads it as an end even though it escapes with \"
See the result below. The last line ends as "<script
type="text/javascript</div>"
I'm reporting this as a bug because it presents an unexpected outcome to an execution that
should work differently (for the purpose it was created). I could, in fact, simply retain the
original \n coding and do an str_replace for it.
Also, the example I gave is the way I use it. There are other ways new lines can be used with
parse_ini_file. Its usage, for how it's going to be used, can become debatable that will
eventually come down to "choice" (developer) and "control" (programmer).
Test script:
---------------
# PHP script
$var = parse_ini_file ('file.ini');
$var .= '</div>';
echo $var;
# ini file
google_ad = "<script type=\"text/javascript\"><!--
google_ad_client = \"xxx\";
google_ad_width = 728;\ngoogle_ad_height = 90;
//-->
</script>
<script type=\"text/javascript\"
src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>"
Actual result:
--------------
<script type="text/javascript"><!--
google_ad_client = "xxx";
google_ad_width = 728;\ngoogle_ad_height = 90;
//-->
</script>
<script type="text/javascript</div>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55091&edit=1