Good day, gentlemen connoisseurs!

I have a block-div, with the class b-cat-marker js-schedule . It has a data-ch attribute that contains the information I need, looks like this:

 <div class="b-cat-market js-schedule" data-ch="{"49":{"id":"49","title":"Первый канал - Евразия","logo":{"width":"38","height":"30","src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333708610295M22586\/orig"}},"785":{"id":"785","title":"КТК","logo":{"width":"38","height":"30","src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333712749673M29386\/orig"}},"528":{"id":"528","title":"Астана","logo":{"width":"38","height":"30","src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333708111007M36576\/orig"}},"490":{"id":"490","title":"Седьмой канал","logo":{"... ndex.net \/get-tv-channel-logos\/1336217773495M65560\/orig"}},"614":{"id":"614","title":"Кухня ТВ","logo":{"width":"38","height":"30","src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333708867834M98854\/orig"}},"153":{"id":"153","title":"Авто Плюс","logo":{"width":"38","height":"30","src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333715566443M50909\/orig"}},"250":{"id":"250","title":"365","logo":{"width":"38","height":"30","src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333713180776M42789\/orig"}}}"> 

As you already understood, I need to miraculously turn this attribute into an array, for example

 $data["position"]=49; $data["id"]=49; $data["title"]="....."; 

PS: no experience in regexp.

  • same json (true invalid). what have the regexp? you need to fix it, if it is automatically generated, correct the generator, send it to the server, then convert it into an array. - Yura Ivanov

2 answers 2

@Yura Ivanov is absolutely right - this is a json presentation of data. If we bring this data into a readable form, we get the following picture:

 { "49":{ "id":"49", "title":"Первый канал - Евразия", "logo":{ "width":"38", "height":"30", "src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333708610295M22586\/orig" } }, "785":{ "id":"785", "title":"КТК", "logo":{ "width":"38", "height":"30", "src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333712749673M29386\/orig" } }, "528":{ "id":"528", "title":"Астана", "logo":{ "width":"38", "height":"30", "src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333708111007M36576\/orig" } }, "490":{ "id":"490", "title":"Седьмой канал", "logo":{ "... ndex.net \/get-tv-channel-logos\/1336217773495M65560\/orig" // (!!!) Смотрим на эту строку } }, "614":{ "id":"614", "title":"Кухня ТВ", "logo":{ "width":"38", "height":"30", "src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333708867834M98854\/orig" } }, "153":{ "id":"153", "title":"Авто Плюс", "logo":{ "width":"38", "height":"30", "src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333715566443M50909\/orig" } }, "250":{ "id":"250", "title":"365", "logo":{ "width":"38", "height":"30", "src":"http:\/\/ avatars.yandex.net \/get-tv-shows\/1333713180776M42789\/orig" } } } 

In the line marked with a comment, when generating, an error is clearly made, namely, the "src" key is missing. If we add it in handles and put all the data into a variable, for example $ str , then it will not be difficult to get an array:

 print_r(json_decode($str, true)); 

What happens in the end, you can see here (go to the tab "Screen")

  • @Deonis, thank you very much for the detailed info, opened my eyes. I'm happy guys !. - Vfvtnjd

If you parse something:

 preg_match_all('#data-ch="(.*)"#',$data,$matches); foreach($matches[1] as $str){ print_r(json_decode($str, true)); } 

well, anyway, just as @Deonis , json_decode + validity json said