ajax - Can't figure out with character encoding in PHP -
i have put little utility reading youtube video tags. http://www.daviddresden.com/tagreader/
<?php header("content-type: application/json"); error_reporting(e_error | e_parse); $_post['fn']='https://www.youtube.com/watch?v=ogat8ehg0eo'; if(isset($_post['fn']) && $_post['fn'] != ''){ $url = htmlentities($_post['fn']); $page_content = file_get_contents('https://www.youtube.com/watch?v=ogat8ehg0eo'); $dom_obj = new domdocument(); if($dom_obj->loadhtml($page_content)){ $dom_obj->loadhtml($page_content); $meta_val = ''; foreach($dom_obj->getelementsbytagname('meta') $meta) { if($meta->getattribute('property')=='og:video:tag'){ $meta_val = $meta_val.','.$meta->getattribute('content'); } } echo substr($meta_val,1); } else{ echo "invalid url!"; } } else{ echo "empty url!"; } ?>
it works asci characters utf characters show unreadable. can't find problem.
converts string iso-8859-1 characters encoded utf-8 single-byte iso-8859-1
use utf8_decode
output:
echo utf8_decode(substr($meta_val,1)) ;
set content-type
utf-8
header('content-type: text/html; charset=utf-8');
full code:
header('content-type: text/html; charset=utf-8'); $_post['fn']='https://www.youtube.com/watch?v=ogat8ehg0eo'; if(isset($_post['fn']) && $_post['fn'] != ''){ $url = htmlentities($_post['fn']); $page_content = file_get_contents('https://www.youtube.com/watch?v=ogat8ehg0eo'); $dom_obj = new domdocument(); if($dom_obj->loadhtml($page_content)){ $dom_obj->loadhtml($page_content); $meta_val = ''; foreach($dom_obj->getelementsbytagname('meta') $meta) { if($meta->getattribute('property')=='og:video:tag'){ $meta_val = $meta_val.','.$meta->getattribute('content'); } } echo utf8_decode(substr($meta_val,1)) ; } else{ echo "invalid url!"; } } else{ echo "empty url!"; }
Comments
Post a Comment