Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取不带标题/编码的外部网页的html源_Php_Html_Character Encoding - Fatal编程技术网

Php 获取不带标题/编码的外部网页的html源

Php 获取不带标题/编码的外部网页的html源,php,html,character-encoding,Php,Html,Character Encoding,我只是想知道是否可以从html文件中提取编码的内容(utf-8),而不需要编码头 我的具体案例是这个网站: 我想提取所有信息,但如您所见,例如,这个词看起来很糟糕: 莫特·瑞德 我试着用不同的选项来使用file_get_html、htmlentities、utf_decode、utf_encode和混合它们,但我找不到解决方案 编辑: 我只想通过以下简单代码看到格式正确的同一个网站: $html_discos = file_get_html("http://www.metal-archives

我只是想知道是否可以从html文件中提取编码的内容(utf-8),而不需要编码头

我的具体案例是这个网站:

我想提取所有信息,但如您所见,例如,这个词看起来很糟糕:

莫特·瑞德

我试着用不同的选项来使用file_get_html、htmlentities、utf_decode、utf_encode和混合它们,但我找不到解决方案

编辑:

我只想通过以下简单代码看到格式正确的同一个网站:

$html_discos = file_get_html("http://www.metal-archives.com/band/discography/id/223/tab/all");
//some transform/decode here
print_r($html_discos);
我希望字符串或DOM对象中的内容格式正确,以便稍后获得某些部分

编辑2:

$file_get_html是“简单html dom”库的一个函数:

具有此代码的:

function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
{
    // We DO force the tags to be terminated.
    $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
    // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
    $contents = file_get_contents($url, $use_include_path, $context, $offset);
    // Paperg - use our own mechanism for getting the contents as we want to control the timeout.
    //$contents = retrieve_url_contents($url);
    if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
    {
        return false;
    }
    // The second parameter can force the selectors to all be lowercase.
    $dom->load($contents, $lowercase, $stripRN);
    return $dom;
}

尝试使用
html\u eneity\u decode
(该页面的源代码包含编码字符)

URL的内容类型

http://www.metal-archives.com/band/discography/id/203/tab/all
是:

这将默认为ISO-8859-1。但是您希望使用UTF-8。更改内容类型,使其正确显示:

Content-Type: text/html; charset=utf-8
见:


只要您以UTF-8的形式发射,原始数据就会正常工作。

我在浏览器中也这么认为,因此问题可能出在站点端。您的问题不清楚问题所在。显示您的代码,并给出一个简洁的示例,在该示例中,您可以看到无效数据,并且在其中显示string.PHP没有任何
file\u get\u html
函数的编码。除非您没有分享关于该函数的任何细节,否则从您当前给出的代码示例来看,就没什么可说的了。一个简单的
标题('Content-Type:text/html;charset=utf-8')在任何输出完成之前可能已经完成了这项工作,但这只是猜测。好吧,我已经怀疑您正在使用简单的HTML DOM库,正如现在显示的,您正在使用。认为它被打破了。相反,使用PHP附带的
DOMDocument
,看看这个关于如何加载UTF-8编码网站的问题:据我所知,他并不控制这个问题website@ExplosionPills当前位置我想知道问题到底是什么。这是一个外部网站,我无法控制网站。编辑:问题是如何查看正确的格式(如果可能的话)当然可以,但你可以阅读。只需将输入作为UTF-8(它是UTF-8)就可以了。在你的网站上表明你有UTF-8。你不需要再做什么了。否则,每个文本字符串调用一次
utf\u decode
将utf-8字符串转换为ISO-8859-1编码字符串。但是,您需要用输出信号表明您的数据是ISO-8859-1编码的,否则它将(再次)被破坏。尝试:$html\u discos=file\u get\u html(“);$html\u discos=html\u entity\u decode($html\u discos);print\r($html\u discos);看到相同的。。。
Content-Type: text/html; charset=utf-8
header('Content-Type: text/html; charset=utf-8');
echo file_get_contents('http://www.metal-archives.com/band/discography/id/203/tab/all');