我想在配置文件中保留某个字符串,以便由 PHPparse_ini_file()
函数解析。但是,此字符串包含一些需要以某种方式编码的特殊字符(带有类似0x2C
or的代码)。0x3D
有没有办法在这样的文件中用十六进制代码编写特殊字符?
1 回答
The proper way to escape INI values is to enclose them in "double quotes"
. If your string doesn't contain double quotes, you can use it in as a value enclosed in double quotes.
Escaping single quotes with a backslash seems to work as long as there are not two consecutive double quotes in the value, as per http://php.net/manual/en/function.parse-ini-file.php#100046
If you want to do your own escaping, you certainly can:
htmlspecialchars
/ htmlspecialchars_decode
escapes <
,>
,&
and "
.
htmlentities
/ html_entitity_decode
will escape very aggresively (but also very safely) to HTML entities
urlencode
/ urldecode
will escape all special characters except _-~.
.
base64_encode
/ base64_decode
will ensure the encoded string contains only alphanumeric characters and +=/
. This might be optimal for encoding binary data but doesn't preserve readability.