C
C
CoolBatman2732017-08-25 16:19:09
PHP
CoolBatman273, 2017-08-25 16:19:09

How to find and convert data from a script tag, which is in json format, into a php array?

<script id="data" type="application/json">
    [
    	{
    		"slide_1":"img/viewer/Winterfell.jpg",
    	},
      {
    		"slide_1":"img/viewer/The_Eyrie.jpg",
    	},
      {
    		"slide_1":"img/viewer/Riverrun.jpg",
    	},
      {
    		"slide_1":"img/viewer/Pyke.jpg",
    	},
    ]
    </script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pushkarev, 2017-08-25
@AXP-dev

$html = '....';
preg_match('@<script(?:[^>]*\stype="(?:application|text)/json")?[^>]*>(.*?)</script>@s', $html, $data);
$data = json_decode($data[1]);

print_r($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question