protected function FeedsExXml::convertEncoding in Feeds extensible parsers 7.2
Converts a string to UTF-8.
Requires the iconv, GNU recode or mbstring PHP extension.
Parameters
string $data: The string to convert.
string $encoding: The encoding to convert to.
Return value
string The encoded string, or the original string if encoding failed.
Overrides FeedsExBase::convertEncoding
See also
3 calls to FeedsExXml::convertEncoding()
- FeedsExHtml::convertEncoding in src/
FeedsExHtml.inc - Converts a string to UTF-8.
- FeedsExQueryPathHtml::convertEncoding in src/
FeedsExQueryPathHtml.inc - Converts a string to UTF-8.
- FeedsExXml::prepareDocument in src/
FeedsExXml.inc - Prepares the DOM document.
2 methods override FeedsExXml::convertEncoding()
- FeedsExHtml::convertEncoding in src/
FeedsExHtml.inc - Converts a string to UTF-8.
- FeedsExQueryPathHtml::convertEncoding in src/
FeedsExQueryPathHtml.inc - Converts a string to UTF-8.
File
- src/
FeedsExXml.inc, line 187 - Contains FeedsExXml.
Class
- FeedsExXml
- Parses XML documents with XPath.
Code
protected function convertEncoding($data, $encoding = 'UTF-8') {
// Check for an encoding declaration in the XML prolog.
$matches = FALSE;
if (preg_match('/^<\\?xml[^>]+encoding\\s*=\\s*("|\')(.+?)(\\1)/', $data, $matches)) {
$encoding = $matches[2];
}
elseif ($detected = parent::detectEncoding($data)) {
$encoding = $detected;
}
// Unsupported encodings are converted here into UTF-8.
$php_supported = array(
'utf-8',
'us-ascii',
'ascii',
);
if (in_array(strtolower($encoding), $php_supported)) {
return $data;
}
$data = parent::convertEncoding($data, $encoding);
if ($matches) {
$data = preg_replace('/^(<\\?xml[^>]+encoding\\s*=\\s*("|\'))(.+?)(\\2)/', '$1UTF-8$4', $data);
}
return $data;
}