PHP 中的 register_printf_function
我需要让用户为使用 vsprintf 的函数指定自定义格式,并且由于 PHP 没有 glibc' register_printf_function(),所以我必须使用 PCRE 来完成。
我的问题是,以可用于编程使用的方式匹配 % 后跟任何字符且前面没有 % 的最佳 REGEXP 是什么?
我能得到的最接近的解决方案是:
<?php
function myprintf($format,$args) {
$matches = array();
preg_match_all('/((?<!%)%*[^%]+)/', $format,$matches,PREG_OFFSET_CAPTURE|PREG_PATTERN_ORDER);
print_r($matches);
}
myprintf("begin%a%%b%%%c%d",NULL);
哪种可行,但是这会被“%%%c”等输入“混淆”。我希望在一个分组中有一系列两个 % 符号(即转义),例如:
Array (
0 => '%%',
1 => '%c'
)
而不是像现在这样: 大批 ( 0 => '%%%c' ) 也就是说,尽管已标记化,但我需要保持输入完整,以便在处理输入中遇到的自定义 printf 格式后将各个部分连接在一起。
谢谢,
Flavius
PS:“用户”实际上是另一个程序员。我了解安全隐患。
I need to let the user specify a custom format for a function which uses vsprintf, and since PHP doesn't have glibc' register_printf_function(), I'll have to do it with PCRE.
My question is, what would be the best REGEXP to match % followed by any character and not having % before it, in an usable manner for programmatic use afterwards?
The closest solution I could get was:
<?php
function myprintf($format,$args) {
$matches = array();
preg_match_all('/((?<!%)%*[^%]+)/', $format,$matches,PREG_OFFSET_CAPTURE|PREG_PATTERN_ORDER);
print_r($matches);
}
myprintf("begin%a%%b%%%c%d",NULL);
Which kinda works, BUT this gets "confused" by inputs like "%%%c". I would like to have series of two %-signs (that is, escaped) in one grouping, like:
Array (
0 => '%%',
1 => '%c'
)
and not like it's doing it now:
Array (
0 => '%%%c'
)
That is, I need to keep the input intact, though tokenized, in order to join the pieces together after I do the processing of the custom printf formats I encounter in the input.
Thanks,
Flavius
PS: the "user" is actually another programmer. I am aware of the security implications.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码:
输出:
这也应该正确解析复合格式说明符,例如
%.3f
或%$1d
。Code:
Output:
This should parse compound format specifiers like
%.3f
or%$1d
properly as well, also.如果您想要的是一个 % 后跟一个字母或另一个 %,那么您可以简单地执行以下操作:
编辑:
我认为这相当于您从下面的注释中得到的内容:
主要区别在于
[2]=> string(3) "%%b"
变为[2]=>;字符串(2) "%%" [3]==> string(1) "b"
应该会给出相同的结果,因为%%
无论如何都会被评估为单个%
。If what you want is a % followed by a letter or another % then you can simply do:
Edit:
I think this is equivalent to what you want from the comments below:
The main difference is that
[2]=> string(3) "%%b"
becomes[2]=> string(2) "%%" [3]=> string(1) "b"
which should give you the same results because the%%
would be evaluated as a single%
anyways.