从print_r输出创建变量 [英] Create variable from print_r output

查看:92
本文介绍了从print_r输出创建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从print_r输出创建变量?换句话说,我想知道php中是否存在类似于我的虚拟var_import函数的内容? var_import将是var_export

How can i create variable from it's print_r output ? In other words, i'd like to know if something similar to my fictive var_import function exists in php ? var_import would be the inverse of var_export

他是一个用例:

$a = var_import('Array ( [0] => foo [1] => bar )');
$output = var_export($a);
echo $output; // Array ( [0] => foo [1] => bar )

如果不存在这样的功能,是否有工具(或在线工具)来执行此操作? 我也有兴趣对var_dump输出执行相同操作.

If such a function does not exist, is there a tool (or online tool) to do this ? I am also interested to do the same with var_dump output.

该变量仅可作为print_r输出(字符串)使用.为阐明我的需要,请想象以下情况:某人在Internet上的某个位置以print_r输出发布了一些示例.要测试他的代码,需要将他的print_r变量import放入代码中.这是var_import有用的示例.

The variable is only available as a print_r output (string). To clarify what i need, imagine the folowing situation: someone posts a some sample on the internet somewhere with a print_r output. To test his code, you need to import his print_r variable into your code. This is an example where var_import would be usefull.

推荐答案

有趣的是,PHP手册包含一个示例,该示例尝试从print_r输出重新创建原始结构:
print_r_reverse()
http://www.php.net/manual/zh/function .print-r.php#93529

Amusingly the PHP manual contains an example that tries to recreate the original structure from the print_r output:
print_r_reverse()
http://www.php.net/manual/en/function.print-r.php#93529

但是,它确实取决于保留的空格.因此,您将需要实际的HTML内容,而不是将呈现的文本通过管道传递回去.

However it does depend on whitespace being preserved. So you would need the actual HTML content, not the rendered text to pipe it back in.

此外,除了数组,它看起来不懂其他任何东西,并且不会下降.这将是非常困难的,因为没有真正的字符串终止标记,它既可以包含换行符又可以包含)甚至[0]=>,而这些可能被误认为是print_r分隔符.即使使用递归正则表达式(和实际的解析器),正确地解析print_r结构也几乎是不可能的-只能通过像上面链接的代码中那样的猜测工作拆分来实现.(那里还有两个版本,也许您必须尝试他们来找到适合您特定情况的匹配项.)

Also it doesn't look like it could understand anything but arrays, and does not descend. That would be incredibly difficult as there is no real termination marker for strings, which can both contain newlines and ) or even [0] and => which could be mistaken for print_r delimiters. Correctly reparsing the print_r structure would be near impossible even with a recursive regex (and an actual parser) - it could only be accomplished by guesswork splitting like in the code linked above.. (There are two more versions there, maybe you have to try them through to find a match for your specific case.)

这篇关于从print_r输出创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆