如何通过PHP parse_ini_file函数解析文件中的特殊字符? [英] How to escape special characters in a file parsed by PHP parse_ini_file function?
问题描述
我想在一个配置文件中保留某个字符串,即由PHP解析 parse_ini_file()
功能。但是,该字符串包含一些特殊字符(代码如 0x2C
或 0x3D
),需要以某种方式进行编码。有没有办法用这样的文件中的十六进制代码写一个特殊字符?
I'd like to keep a certain string in a configuration file, that is to be parsed by PHP parse_ini_file()
function. However, this string contains some special characters (with codes like 0x2C
or 0x3D
) that need to be encoded in some way. Is there any way to write a special character with a hex code in such a file?
推荐答案
正确的方式来转义INI值将它们包含在双引号
中。如果您的字符串不包含双引号,则可以将其作为双引号括起来使用。
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.
使用反斜杠转义单引号似乎只要工作根据 http: //php.net/manual/en/function.parse-ini-file.php#100046
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
转义
,>
&
和。
htmlentities
/ html_entitity_decode
将非常积极地(但也非常安全地)转移到HTML实体
htmlentities
/ html_entitity_decode
will escape very aggresively (but also very safely) to HTML entities
urlencode
/ urldecode
将会转义除 _-〜。
之外的所有特殊字符。
urlencode
/ urldecode
will escape all special characters except _-~.
.
base64_encode
/ base64_decode
将确保编码的字符串只包含字母数字字符和 + = /
。这对于编码二进制数据可能是最佳的,但不能保持可读性。
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.
这篇关于如何通过PHP parse_ini_file函数解析文件中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!