Escape hyperlink with exclamation marks in php.ini
I have a config file that takes text warnings like follows:
warnings.1 = Please check the date
These are presented to the user as HTML. I need to embed a hyperlink like the following:
warnings.1 = <a href="http://f开发者_如何学Pythonoo.com/!FOO!/">check with foo</a>
I can't for the life of me figure out how to escape this such that parse_ini_file() can read it and get that string the way I want.
http://php.net/manual/en/function.parse-ini-file.php:
array parse_ini_file(string $filename [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL]])
scanner_mode:
Can either be INI_SCANNER_NORMAL (default) or INI_SCANNER_RAW. If INI_SCANNER_RAW is supplied, then option values will not be parsed.
Changelog: 5.3.0 Added optional scanner_mode parameter.
So you're screwed if you're not on 5.3. Or you need to use a different implementation, parse_ini_file is not the only INI file parser for PHP.
Try using urlencode, the exclamation marks should be escaped as such:
urlencode("foo.com/!FOO!/");
Output:
foo.com%2F%21FOO%21%2F
I've ended up switching to Zend_Config_Xml as it's a lot clearer to our developers how to escape data than trying to work out the slightly weird .ini syntax.
Thanks to all for answers.
I encountered a somewhat similar scenario just recently and after some thought I arrived at the conclusion that the problem is not with the function or class being used but with the use itself. Parameter values such as
<a href="http://foo.com/!FOO!/">check with foo</a>
would be better implemented in a way that only the value of the href
is the value of the parameter. Example:
warnings.1.href = "http://foo.com/!FOO!/"
warnings.1.text = "check with foo"
This is because the parameter values should only contain values that are bound to change (from instance to instance). In your case, the anchor tags are better left in the html itself, and not in the .ini
configuration file. This does not only keep your configuration file lightweight, but it also keeps it clean because of the separation of concerts wherein the HTML code should be in the HTML and the configuration values in the configuration file.
So yeah, there's no need to switch PHP versions or adding parameters to the parse_ini_file
function (since doing so would be a hack to the Zend_Config_Ini class, which would be better done as an official patch if necessary).
精彩评论