You are here

public function XmlEncoder::convertEncoding in Feeds extensible parsers 8

Converts a string to UTF-8.

Parameters

string $data: The string to convert.

Return value

string The encoded string, or the original string if encoding failed.

Overrides TextEncoder::convertEncoding

File

src/Encoder/XmlEncoder.php, line 34

Class

XmlEncoder
Converts the encoding of an XML document to UTF-8.

Namespace

Drupal\feeds_ex\Encoder

Code

public function convertEncoding($data) {

  // Check for an encoding declaration in the XML prolog.
  $matches = FALSE;
  $encoding = 'ascii';
  if (preg_match($this->findRegex, $data, $matches)) {
    $encoding = $matches[2];
  }
  elseif ($detected = $this
    ->detectEncoding($data)) {
    $encoding = $detected;
  }

  // Unsupported encodings are converted here into UTF-8.
  if (in_array(strtolower($encoding), self::$utf8Compatible)) {
    return $data;
  }
  $data = $this
    ->doConvert($data, $encoding);
  if ($matches) {
    $data = preg_replace($this->replaceRegex, $this->replacePattern, $data);
  }
  return $data;
}