0

I have a remote page, below shows its source code,

<!DOCTYPE html>
<html>
<head>
    <title>somethingg blabla</title>
</head>
<body>
<script type="text/javascript">
    function blala( ) { /*** code **/ }
    var sitedata = {
            "count": 1,
            "threads": {
                "38752": {
                    "thread_id": 38752,
                    "node_id": 4,
                    "title": "The ShadyCraft Beta Launch!", 
                    "prefix_id": 19, 
                    "content": {
                        "count": 1,
                        "content": {
                            "226167": {
                                "post_id": 226167

                            }
                        }
                    }
                }
            }
        };
var extra_codes_here = 'blabla';
</script>

<h1>Hello world</h1>
</body>
</html>

I have code for curl this page and i got this source code, but how to get the value var sitedata to php array ,

2
  • you want javascript variable in to php array? Commented Dec 30, 2016 at 5:47
  • yes i want javascript to php array Commented Dec 30, 2016 at 12:03

1 Answer 1

1
$doc; // your document

preg_match('/var\ssitedata\s=\s({.*});/s', $doc, $matches);
$json = $matches[1];
print_r(json_decode($json, true));

and your json is incorrect.

"post_id": 226167, 

to

"post_id": 226167
Sign up to request clarification or add additional context in comments.

2 Comments

this json just copied from internet.. its just a sample.. :)
copied json is too big then stackoverflow shows too many codes... just removed some parts ;)

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.