0

I have a problem with parsing a HTML page. There is a variable in javascript that I need to get it and put it into json format.

<script>
  var variable1 = "foo";
  var variable2 = "boo";
  var multipleArray = [
            {"var1":"1",
             "var2": [{
                 "var21":"Extra Lge",
                 "var22":"45923090470",
                 "var23": {"key1":"value1",
                           "key2":"value"}
                 }],
                 ...etc...
             }]
</script>

Is there any easy way, how to get var multipleArray and all keys and values ? I used preg_match()

preg_match("'var multipleArray = [\{.*?\}]'", $source, $matches );

But it returns $matches as empty array().

What i am doing wrong? My experence with reg.ex. is on level 0 :(

I'll be glad for any help :) Thank you so much :)

3
  • you should just pass the variable via ajax in pure json format and then capture it on php side and consume it. Commented Aug 20, 2013 at 7:17
  • I cannot. Because i am parsing external web site. Commented Aug 20, 2013 at 7:25
  • you should probably use something like this developer.mozilla.org/en-US/docs/Rhino Commented Aug 20, 2013 at 7:28

1 Answer 1

0

With th following pattern you should get the Array without the var multipleArray identifier.

preg_match('/\[(\{(.*)\})\]/', $source, $matches );

Now to get the key value pairs you have to parse it. Or if you want to have a quick, dirty and evil win you could do the following:

You use str_replace() to replace every ':' with '=>', '[{' with '[', '}]' with ']', '{' with '[', '}' with ']'.

With the result-string ($result) you do the following:

$result_array = null;
eval('$result_array = '.$result);

Now $result_array contains your Array.

Your Answer

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.