V
V
Vlad2016-04-12 13:19:38
PHP
Vlad, 2016-04-12 13:19:38

How to fix the encoding in when parsing?

I'm trying to ask the page, but at the output I get the wrong encoding in cracks.
My site is on windows-1251
How to solve the problem?

$file = file_get_contents('https://tv.yandex.ru/213/channels/150?date=2016-04-12&period=all-day');
                    $dom = new DOMDocument('1.0', 'utf-8');
                    $dom->loadHTML($file);
                    $xpath = new DOMXPath($dom);
                    $schedule_items = $xpath->query('//div[@class="b-tv-channel-schedule__item"]');
                    $count = $schedule_items->length;

                    if ($count > 0) {
                        for ($i = 0; $i < $count; $i++) {
                            $item = $schedule_items->item($i);
                            $event_time = $xpath->query('a/span[@class="tv-event__time"]/span[@class="tv-event__time-text"]', $item);
                            $event_title = $xpath->query('a/div[@class="tv-event__title"]/div[@class="tv-event__title-inner"]', $item);
                            if ($event_time->length > 0 && $event_title->length > 0) {
                                $result .= $event_time->item(0)->nodeValue.$event_title->item(0)->nodeValue;
                            }
                        }
                        $teleprogramma .= "<h3><a href='#'>".$day_title."</a></h3><div>".$result."</div>";
                    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2016-04-12
@alexey-m-ukolov

My site is on windows-1251

$dom = new DOMDocument('1.0', 'utf-8');

V
Vit, 2016-04-12
@fornit1917

convert the parsed data from utf8 to cp1251 with iconv

K
krypt3r, 2016-04-12
@krypt3r

$result .= $event_time->item(0)->nodeValue . ' ' . mb_convert_encoding($event_title->item(0)->nodeValue, 'CP1251', 'UTF-8');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question