What's the opposite of var-export function in php?

耗尽温柔 提交于 2021-02-16 21:27:18

问题


I export a variable to a textarea via "var_export($schools,true)" so user can edit it. Then I want to 'update' the variable with the changes made. The updates is received via POST method.

I have some text that I want to become a variable. How can I do that?

What I do right now is that I edit the variable manually in .php file. I want to give an web interface to users do to the same. There won't be no security issues as this will be strictly inhouse tool only.

Sample of the variable

$schools = array(

    "PHCS"=> array(
        "full_name"=> "Pacific Hills Christian School",
        "version"=> "4.0.2b",
        "etc"=> "etc"
      ),

    "WAC"=> array(
        "full_name"=> "Wollondilly Anglican College",
        "version"=> "4.0.1",
        "etc"=> "etc"
      ),
  );

回答1:


You would be looking at using eval() which use of is pretty controversial due to security risks.

I would suggest you use serialize() and unserialize(), or even better, the JSON functions instead.

The JSON encode/decode would be the best option for displaying to the user as it's fairly readable.




回答2:


Following the advice on php.net, if you plan to change the object you should use serialize and unserialize:

$var = serialize(array('hello'));
// string(22) "a:1:{i:0;s:5:"hello";}"
var_dump( unserialize($var) );


来源:https://stackoverflow.com/questions/9323485/whats-the-opposite-of-var-export-function-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!