Stomped
Stomped

Reputation: 2100

Turn Javascript String into PHP array

I'm not talking about JSON. I've got a program with the input being a javascript data structure in string format, something like this:

$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? 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.

Just wondering if there's a better way to do it.

Upvotes: 0

Views: 414

Answers (2)

Todd Moses
Todd Moses

Reputation: 11029

You can post it to PHP as a string delimited by some character:

$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.

Upvotes: 0

David Pfeffer
David Pfeffer

Reputation: 39833

Nope, you pretty much hit on the best way to do it.

Upvotes: 3

Related Questions