You are here

public function FeedsExXmlEncoder::convertEncoding in Feeds extensible parsers 7

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 FeedsExTextEncoder::convertEncoding

File

src/Xml/Utility.php, line 167
Contains FeedsExXmlUtility.

Class

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

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