Hello. Please tell me, here is the task to get the value of the title tag from another site, the site is not known in advance, it can be absolutely anything. How i do it now

$dom = new DOMDocument(); $tag = 'title'; @$dom->loadHTML(@file_get_contents('http://site.com/')); if ($dom->getElementsByTagName($tag)->length > 0) $title = $dom->getElementsByTagName($tag)->item(0)->textContent; else $title = ''; 

But sites on which the windows-1251 encoding gives me the title in this form

 Site - Ðåêëàìíîå àãåíòñòâî. Ýôôåêòèâíàÿ ðàñêðóòêà ñàéòîâ 

I tried to use the functions mb_convert_encoding () and iconv () with different parameters, but none of them gave the desired result. Please tell me how you can get a readable title where the windows-1251 encoding

  • Show how you used mb_convert_encoding () or iconv () in this code. - Visman September
  • After getting the value in the title variable, I created another variable to which I entered the result of performing these functions, and all I changed there was from which encoding to which process to do - Angus123
  • I think that you need to recode the result of the function file_get_contents() into UTF-8 and then load the data into the DOMDocument . - Visman

1 answer 1

 $str='Ðåêëàìíîå àãåíòñòâî. Ýôôåêòèâíàÿ ðàñêðóòêà ñàéòîâ'; $str = iconv('utf-8', 'cp1252', $str); $str = iconv('cp1251', 'utf-8', $str); echo $str; //Рекламное агентство. Эффективная раскрутка сайтов 

Test online: http://sandbox.onlinephpfunctions.com/code/285d0149220b35b140504c32d7ec6eb26f7a9c54