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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示