Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:01:40+00:00 2026-05-28T14:01:40+00:00

I need to scrape some data from webpages. But I have some encoding problems

  • 0

I need to scrape some data from webpages. But I have some encoding problems with it.

Here is just a litle sample code to show the problem on a well known german webpage.

I expected to get this text from the webpage:
Alle Kritiker werden gespannt nach Wolfsburg schauen, denn der VfL wurde kräftig umgekrempelt. Können die Kölner daraus ihren Nutzen ziehen?

But as you can see in my tests, I get this:
Alle Kritiker werden gespannt nach Wolfsburg schauen, denn der VfL wurde kräftig umgekrempelt. Können die Kölner daraus ihren Nutzen ziehen?

The meta tag of the page says, that it is UTF-8 encoded…
And mb_detect_encoding also says, that it is UTF-8.

But why I get this crappy text back?

And when I convert the text to ISO-8859-1 I get the expected result…

<?php
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';

$url = "http://www.goal.com/de/match/60952/wolfsburg-vs-1-fc-k%C3%B6ln/preview";

$fileContent = @file_get_contents($url);

$dom = @DOMDocument::loadHTML($fileContent);
$xpath = new DOMXpath($dom);

$element = $xpath->query(".//*[@id='article_headline']/h2");
if ($element->length > 0) {
  $item = $element->item(0);

  $text = $item->textContent;
  echo $text . "<br>";

  $text =  iconv("UTF-8", 'ISO-8859-1', $text);
  echo $text . "<br>";
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:01:41+00:00Added an answer on May 28, 2026 at 2:01 pm

    DOMDocument’s html parser (which is libxml2) will try to guess the encoding of the input if it comes across malformed html. Usually it does a pretty good job but this page seems to be a pathological case. Perhaps the presence of east Asian characters is confusing it.

    In situations like these where you are absolutely sure you know the encoding you can force the text into 7-bit ascii before feeding it to the loadHTML() method. You can do this like so:

    $fileContent = mb_convert_encoding($fileContent, 'HTML-ENTITIES', 'UTF-8');
    

    This will convert all non-ascii characters into an html named or numeric character entity. The page works properly for me when I do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this situation where I need to get some data from a webpage
I am trying to scrape some data from a page with a table based
I want to scrape string data from some binary text files that contain embedded
I need to scrape some content from a HTTP response with Java. The required
I have a python function that scrapes some data from a few different websites
Hey, I'd like to scrape some data from my blog using YQL: SELECT *
I need to scrape Form 10-K reports (i.e. annual reports of US companies) from
need ask you about some help. I have web app running in Net 2.0.
Need some help about with Memcache. I have created a class and want to
I would like to scrape some dynamic data off of a website. On the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.