php - Getting charset of page and convert it -


creating preview url in post message, on fb. on post url in textarea preview url shown below. title, description, image, etc.

had problem charset encoding of different pages. page in utf-8, , if preview url not in utf-8, incorrect text shown on page. needed charset of url, convert utf-8 , needed info , show on page.

actually stackoverflowed )) didn't find method this.

finally did this, , works nice. please check if right , shorter way it.

$post_url='http://website.domain';  /// headers //////////////////////////////////////////////////////////// function get_headers_curl($url)  {      $ch = curl_init();       curl_setopt($ch, curlopt_url,            $url);      curl_setopt($ch, curlopt_header,         true);      curl_setopt($ch, curlopt_nobody,         true);      curl_setopt($ch, curlopt_returntransfer, true);      curl_setopt($ch, curlopt_timeout,        15);       $r = curl_exec($ch);      return $r;  } $the_header = get_headers_curl($post_url); /// check redirect ///////////////////////////////////////////////// if (preg_match("/location:/i", $the_header)) {     $arr = explode('location:', $the_header);     $location = $arr[1];      $location=explode(chr(10), $location);     $location = $location[0];  $the_header = get_headers_curl(trim($location)); } /// charset ///////////////////////////////////////////////////////////////////// if (preg_match("/charset=/i", $the_header)) {     $arr = explode('charset=', $the_header);     $charset = $arr[1];      $charset=explode(chr(10), $charset);     $charset = $charset[0];     } /////////////////////////////////////////////////////////////////////////////// // echo $charset;  if($charset && $charset!='utf-8') { $html = iconv($charset, "utf-8", $html); } 

if it's ok, may need it. thank you.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -