php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55091 parse_ini_file: last escaped double-quote of multiline ends read
Submitted: 2011-06-30 14:51 UTC Modified: 2020-10-29 15:03 UTC
Votes:4
Avg. Score:2.5 ± 1.1
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: PinoyHarryPotter at gmail dot com Assigned:
Status: Verified Package: Filesystem function related
PHP Version: 5.3.6 OS: Windows XP, CENTOS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: PinoyHarryPotter at gmail dot com
New email:
PHP Version: OS:

 

 [2011-06-30 14:51 UTC] 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>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-29 15:03 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2020-10-29 15:03 UTC] cmb@php.net
> 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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 09:01:30 2024 UTC