Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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!

share|improve this question
    
can't the data be converted to json from js and then set to php ? then you can use json_decode(); – Poelinca Dorin Jan 28 '11 at 11:32
1  
possible duplicate of Parsing javascript arrays in PHP – middaparka Jan 28 '11 at 11:33
    
From what you've said, it's hard to see what the problem is with using JSON? – Spudley Jan 28 '11 at 11:35
up vote 1 down vote accepted

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

share|improve this answer
    
It is a complex array file, so converting it by hand to json may fail. – SteMa Jan 28 '11 at 12:09
    
str_replace("'",'"',$target); str_replace(',]',']',$target); Its worth a try woudln't you say? – Matt Jan 28 '11 at 12:48
    
absolutly right, some things are that easy when you know how... – SteMa Jan 28 '11 at 13:51

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.