V
V
vovanfenix2016-09-11 16:53:42
PHP
vovanfenix, 2016-09-11 16:53:42

How to get an array from js code to php into a variable?

I have text in variable
$data = '

<script>
var isProductPage = true;
var Name = "Coll dress";
var brandName = "Dress 123";
var gender = "Womens";
</script>

';
I need to access each variable in php. Is there something similar to json_decode or do I need to parse with regulars?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victor Pleisun, 2016-09-11
@neovictor

$data = str_replace(array('<script>', '</script>', 'var '), '', $data);
$array_results = explode(";", $data);
if (is_array($array_results)) {
      $stats_data = array();
      foreach ($array_results as $key => $value) {
        if (!$value) {
          continue;
        }
        $_temp = explode(" = ", $value);
        $result_data[$_temp[0]] = str_replace(';', '', $_temp[1]);
      }
}

Something like this

V
vovanfenix, 2016-09-11
@vovanfenix

thanks, it copes well with variables, but the trouble started when parsing the array

<script>
pImgs[23] = {};
pImgs[23]['MULTIVIEW'] = {};
pImgs[23]['MULTIVIEW_THUMBNAILS'] = {};
pImgs[23]['4x'] = {};
pImgs[23]['DETAILED'] = {};
pImgs[23]['2x'] = {};
pImgs[23]['MULTIVIEW_THUMBNAILS']['p'] = '/tpm/main.jpg';
</script>

A
amorphine, 2016-09-11
@amorphine

Can

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question