2013-06-01 20:57:17 +02:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoFeed\Encoding;
|
2013-06-01 20:57:17 +02:00
|
|
|
|
2015-03-01 19:56:11 +01:00
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Encoding class.
|
2015-03-01 19:56:11 +01:00
|
|
|
*/
|
2014-05-20 20:20:27 +02:00
|
|
|
class Encoding
|
|
|
|
{
|
2014-05-25 14:47:03 +02:00
|
|
|
public static function convert($input, $encoding)
|
|
|
|
{
|
2015-01-29 03:57:34 +01:00
|
|
|
if ($encoding === 'utf-8' || $encoding === '') {
|
|
|
|
return $input;
|
2014-05-25 14:47:03 +02:00
|
|
|
}
|
2015-01-29 03:57:34 +01:00
|
|
|
|
2015-06-21 15:56:36 +02:00
|
|
|
// suppress all notices since it isn't possible to silence only the
|
|
|
|
// notice "Wrong charset, conversion from $in_encoding to $out_encoding is not allowed"
|
2015-10-20 04:49:30 +02:00
|
|
|
set_error_handler(function () {}, E_NOTICE);
|
2015-06-21 15:56:36 +02:00
|
|
|
|
|
|
|
// convert input to utf-8 and strip invalid characters
|
|
|
|
$value = iconv($encoding, 'UTF-8//IGNORE', $input);
|
|
|
|
|
|
|
|
// stop silencing of notices
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
// return input if something went wrong, maybe it's usable anyway
|
|
|
|
if ($value === false) {
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2014-05-25 14:47:03 +02:00
|
|
|
}
|
2013-06-01 20:57:17 +02:00
|
|
|
}
|