简体   繁体   English

将Javascript字符串转换为PHP数组

[英]Turn Javascript String into PHP array

I'm not talking about JSON. 我不是在谈论JSON。 I've got a program with the input being a javascript data structure in string format, something like this: 我有一个程序,其输入为字符串格式的javascript数据结构,如下所示:

$string = "
var records = new Array();
records[0] = new Record('data1','data2',data3');
records[1] = new Record('data1','data2',data3');
records[2] = new Record('data1','data2',data3');";

Is there an easy way/library to turn this into a PHP data structure? 有没有简单的方法/库可以将其转换为PHP数据结构? The only way I can think of is to use str_replace to manipulate the string in order to turn it into JSON and then use json_decode. 我能想到的唯一方法是使用str_replace来处理字符串,以便将其转换为JSON,然后使用json_decode。

Just wondering if there's a better way to do it. 只是想知道是否有更好的方法来做到这一点。

不,您几乎是最佳方法。

You can post it to PHP as a string delimited by some character: 您可以将其作为以某些字符分隔的字符串发布到PHP:

$phpArray = explode(",",$postValue);

This is not a better way just another way to do it. 这不是更好的方法,而是另一种方法。 But not without potential problems. 但并非没有潜在的问题。 You have to ensure the delimiter you use is not used in the text and validate. 您必须确保在文本中未使用您使用的定界符并进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM