Search code examples
phpregexsimple-html-dom

How to get the json data under the script tag using html dom?


Here the data i want to get

 <script>
            window['ads_imgs'] = '{"1734275":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/5\/173\/1734275_chevrolet-aveo-5-usi_7.jpg","11570560":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/0\/115\/11570560_audi-a5-sportback-2012-163cp-impecabila-tinuta-in-garaj_14.jpg","3741462":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/2\/374\/3741462_seat-leon-fr-extra-full-dpf_1.jpg","11596338":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/8\/115\/11596338_toyota-auris-12-115-cp-2016_1.jpg","11687842":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/2\/116\/11687842_opel-astra-h-model-2008_1.jpg","11034666":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/6\/110\/11034666_hyundai-tucson-2008_10.jpg","11596195":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/5\/115\/11596195_volkswagen-passat-20-150-cp-2017_1.jpg","10637740":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/0\/106\/10637740_renault-megane-iii-15-dci-90cp-2017_21.jpg","11884407":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/7\/118\/11884407_audi-a4-b7_1.jpg","11889991":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/1\/118\/11889991_vw-touran-19-tdi-105-cp-7-locuri-impecabil-highline_1.jpg","10637645":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/5\/106\/10637645_renault-kadjar-16-dci-130cp-2017_27.jpg","10859634":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/4\/108\/10859634_bmw-z4-35i-cabrio-2009_16.jpg","11889990":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/0\/118\/11889990_vw-passat-20-tdi-170-cp-dsg-automat-highline-full_1.jpg","10803942":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/2\/108\/10803942_mercedes-vito-44_1.jpg","10803920":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/0\/108\/10803920_mercedes-vito-44_11.jpg","11818572":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/2\/118\/11818572_opel-zafira-20l-diesel-7-locuri_4.jpg","11767296":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/6\/117\/11767296_ford-mondeo_4.jpg","9534043":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/3\/953\/9534043_suzuki-sx4_5.jpg","11825640":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/0\/118\/11825640_bmw-520d-f10-automat-m-pachet_1.jpg","11825285":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/5\/118\/11825285_mitshubishi-pajero-pinin-18-gdi_1.jpg","11319696":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/6\/113\/11319696_vw-caddy-4x4-euro5_1.jpg","11358821":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/1\/113\/11358821_bmw-seria-1-e87_1.jpg","11889964":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/4\/118\/11889964_audi-a5-full-led-170cp-schimb-cu-platforma-auto_1.jpg","11675889":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/9\/116\/11675889_audi-a3-s-line-quattro_1.jpg","8642541":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/1\/864\/8642541_toyota-rav-4_1.jpg","11309651":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/1\/113\/11309651_ford-focus_1.jpg","11309545":"https:\/\/media2.lajumate.ro\/media\/i\/cart\/5\/113\/11309545_bmw-318d_1.jpg","11309534":"https:\/\/media1.lajumate.ro\/media\/i\/cart\/4\/113\/11309534_ford-s-max_1.jpg"}';
        </script>

How i can get the id and images link using html dom. Here the code i am getting the exact script but i cant get the value of window['ads_imgs']

                //receiving the data from script tag
               $data = $responseData->find('script',11);

                preg_match("/window['ads_imgs'] = '(.*)'/", $data, $m);
                    print_r($m);
                exit();
            }

Solution

  • There's a small error in your regex. You need to escape the square brackets as follows. You also need to use curly brackets for the enclosure because that's the actual sentinel shown in your "data":

    preg_match("/window\['ads_imgs'\] = '{.*}'/", $data, $m);
    

    You likely just want the json itself though. For this you can do another regex match on $m[0] as follows:

    preg_match("/{.*}/", $m[0], $j_str);
    

    Here's a complete test with sample data:

    $data = "         window['ads_imgs'] = '{\"1734275\":\"https:\/\/media2.lajumate.ro\/media\/i\/cart\/5\/173\/1734275_chevrolet-aveo-5-usi_7.jpg\",\"a\":{\"b\":\"bb\"}}'      ";
    preg_match("/window\['ads_imgs'\] = '{.*}'/", $data, $m);
    preg_match("/{.*}/", $m[0], $j_str);
    $json = json_decode($j_str[0], true);
    print_r($json);
    

    Outputs:

    Array
    (
        [1734275] => https://media2.lajumate.ro/media/i/cart/5/173/1734275_chevrolet-aveo-5-usi_7.jpg
        [a] => Array
            (
                [b] => bb
            )
    
    )