1

If I have a String, containing the Javascript-Code for an array:

parent.data[0].c = [[10,'TESTVALUE',]];

with a lot of nested arrays in it. What is the best way to parse it with PHP. JSON is not an option due to the fact that the data is only available in the format above.

Thx!

asked Jan 28, 2011 at 11:30
3
  • can't the data be converted to json from js and then set to php ? then you can use json_decode(); Commented Jan 28, 2011 at 11:32
  • 1
    possible duplicate of Parsing javascript arrays in PHP Commented Jan 28, 2011 at 11:33
  • From what you've said, it's hard to see what the problem is with using JSON? Commented Jan 28, 2011 at 11:35

2 Answers 2

1

If you remove the extra comma after testvalue, and change 'TESTVALUE' to "TESTVALUE" its valid json

answered Jan 28, 2011 at 11:51
Sign up to request clarification or add additional context in comments.

2 Comments

It is a complex array file, so converting it by hand to json may fail.
str_replace("'",'"',$target); str_replace(',]',']',$target); Its worth a try woudln't you say?
0

I would probably do this with a recursive function that uses regular expression to find matching pairs of [ ] and build the array piece by piece. Probably requires a bit of trial and error not to miss any possible error sources when matching the regular expressions, but should not be too much work, due to recursivity.

Good luck.

answered Jan 28, 2011 at 11:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.