无法弄清楚PHP中的字符编码

时间:2015-04-25 14:23:49

标签: php ajax youtube tags

我已经整理了一些用于阅读youtube视频标签的实用工具。 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') as $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!";
}
?>

适用于ASCI字符,但UTF字符显示不可读。 我找不到问题。

1 个答案:

答案 0 :(得分:1)

utf8_decode -

  

将使用UTF-8编码的ISO-8859-1字符转换为   单字节ISO-8859-1

使用utf8_decode输出:

echo utf8_decode(substr($meta_val,1)) ;

Content-Type设置为utf-8

header('Content-Type: text/html; charset=utf-8');

完整代码:

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') as $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!";
}