duanbin4847 2017-02-27 07:46
浏览 186
已采纳

IBM-850编码如何与PHP函数参数一起使用?

In this code golf answer, aross gives a solution that presumably uses IBM-850 encoding as parameter values in PHP"

echo preg_filter("/^(.*?[aeiouy]+)/i","$1 $1 $0",$argn);
echo preg_filter(~ðíÎÐı└ñ×ÜûÉèåóÈÍðû,~█╬▀█╬▀█¤,$argn);     # Encoded

How does this work? Why are the parameters not quoted? How come only the parameters are encoded and not the rest of the code?

  • 写回答

1条回答 默认 最新

  • douxian6008 2017-02-27 10:10
    关注

    It hasn't so much to do with IBM-850, that's just a codepage filling out the 8th bit beyond ASCII to give a representation to the bytes you'll end up with.

    The key here is the bitwise not operator ~ which flips all the bits - 0 becomes 1, and 1 becomes 0. If you dump ~"/^(.*?[aeiouy]+)/i" to a file and open it up as 850 it'll look like:

    ðíÎÐı└ñ×ÜûÉèåóÈÍðû
    

    And likewise ~"$1 $1 $0" looks like:

    █╬▀█╬▀█¤
    

    So you see where this is headed.

    In PHP an undefined constant is assumed to have a string value matching its name. For example:

    var_dump(foo);
    

    Outputs string(3) "foo" (as well as the notice "Use of undefined constant foo - assumed 'foo'", if notices are on.)

    When either of the two gibberish strings above are put in a PHP script without quotes they're taken as undefined constants with their names assumed for their values as well.

    Now prepend each with ~ to flip their bits back and you've got the original regex and replacement strings:

    preg_filter("/^(.*?[aeiouy]+)/i","$1 $1 $0",$argn)
    

    Only those parameters had their bits flipped because they're the only string literals, which is what this trick applies to. For each string it's shaving off a pair of quotes in exchange for taking on only a single tilde.

    The bit flipping had to be done because either of the original strings on their own without quotes would've landed parse errors.

    Clever way to net two bytes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 有关汽车的MC9S12XS128单片机实验
  • ¥15 求c语言动态链表相关课程有偿,或能将这块知识点讲明白
  • ¥15 FLKT界面刷新异常
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥50 单细胞测序拟时序分析
  • ¥50 运行springboot项目报错
  • ¥15 FTP 站点对站点传输失败
  • ¥15 宝塔面板一键迁移使用不了
  • ¥15 求一个按键录像存储到内存卡的ESP32CAM代码
  • ¥15 如何单独修改下列canvas推箱子代码target参数?,插入图片代替其形状,就是哪个绿色的圆圈每关用插入的图片替代
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部