duanpiangeng8958 2012-10-25 13:13
浏览 129
已采纳

当值包含分号(;)时,使用PHP解析INI文件

I am storing translations in my INI file in my system and they are stored in this manner:

$ini=parse_ini_file('translations.ini',false,INI_SCANNER_RAW);

This INI_SCANNER_RAW setting tells PHP, according to documentation, that:

If INI_SCANNER_RAW is supplied, then option values will not be parsed.

Technically this means that it should not do any parsing with the values in the INI file, so I do not have to quote or escape anything in the INI file. These all work:

example1="one"
example2=one
example1='one'
example3="double quotes ("value")"
example4=double quotes ("value")
example3='double quotes ("value")'

They would output as:

one
one
one
double quotes ("value")
double quotes ("value")
double quotes ("value")

Even this works:

semi-colon1="ˇ1234567890+´õü'äö-.,<>~:_ÖÄ*PÕÜ`?=)(/&%¤#"!@£$€{[]}\½"

Which outputs predictably:

ˇ1234567890+´õü'äö-.,<>~:_ÖÄ*PÕÜ`?=)(/&%¤#"!@£$€{[]}\½

But here's a problem. The very moment I add a semi-colon (;) to my INI value, my parsing breaks, even if I try to escape it.

example1="semi-colon looks like (;) character"
example1="semi-colon looks like (\;) character"
example1="semi-colon looks like (\\;) character"
example1="semi-colon looks like (\\\;) character"

All of the output is:

"semi-colon looks like (
"semi-colon looks like (
"semi-colon looks like (
"semi-colon looks like (

(And the same is true if I use single quotes instead of double-quotes)

My best guess is that this is because semi-colon is considered a character for a comment, so it gets removed and it leaves this snippet of text. The starting quotes remain there because the ending quotes are after the semi-colon, thus it is not encapsulating.

But this makes little sense since # is also considered a comment symbol for INI files.

But this is a pretty serious problem for my system, how can I use a semi-colon in a value string in an INI file? Is this a bug in PHP or expected behavior?

This does not throw an exception or notice or error either.

Thank you!

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,