There is a task to get the value of the title tag of the html document and display it to the user. All anything, with sites in UTF-8 everything is fine, but there are a lot of sites on the Internet. Not all of them are in UTF-8 and very many are displayed as krakozybras. Tell me the right way, how can I get the title value and display it in Russian (if it is in Russian) or in English without cracking? Here is how I tried to solve this problem.
$curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); $content = curl_exec($curl_handle); curl_close($curl_handle); preg_match("/<title>(.*)<\/title>/siU", $content, $matches); $detect = mb_detect_encoding($title, mb_detect_order(), true); $title = iconv($detect, "utf-8", $matches[1]); echo $title;