Gerard Brull
Gerard Brull

Reputation: 1062

Get html source of external webpage without header/encode

I just want to know if its possible to extract content encoded (in utf-8) from a html file without encoding header.

My specific case is this website:

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

I want to extract all the info but, as you can see, this word for example, looks bad:

Motörhead

I tried to use file_get_html, htmlentities, utf_decode, utf_encode and mix of them with different options but I cant find a solution...

Edit:

I just want to see the same website with correct format with this simple code:

$html_discos = file_get_html("http://www.metal-archives.com/band/discography/id/223/tab/all");
//some transform/decode here
print_r($html_discos);

I want the content in correct format in a string or DOM object to get some parts later.

Edit 2:

$file_get_html is a function of "simple html dom" library:

http://simplehtmldom.sourceforge.net/

That have this code:

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;
}

Upvotes: 3

Views: 1363

Answers (3)

Explosion Pills
Explosion Pills

Reputation: 191789

header('Content-Type: text/html; charset=utf-8');
echo file_get_contents('http://www.metal-archives.com/band/discography/id/203/tab/all');

As long as you are emitting as UTF-8, the raw data will work properly.

Upvotes: 1

hakre
hakre

Reputation: 198179

The Content-Type of the URL

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

is:

Content-Type: text/html

This will default to ISO-8859-1. But instead you want to use UTF-8. Change the Content-Type so this is correctly signaled:

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

See: Setting the HTTP charset parameter

Upvotes: 2

Martin Lyne
Martin Lyne

Reputation: 3065

Try using html_eneity_decode http://php.net/manual/en/function.html-entity-decode.php (the source of that page has encoded characters)

Upvotes: 0

Related Questions