Encoding.php 832 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace PicoFeed\Encoding;
  3. /**
  4. * Encoding class.
  5. */
  6. class Encoding
  7. {
  8. public static function convert($input, $encoding)
  9. {
  10. if ($encoding === 'utf-8' || $encoding === '') {
  11. return $input;
  12. }
  13. // suppress all notices since it isn't possible to silence only the
  14. // notice "Wrong charset, conversion from $in_encoding to $out_encoding is not allowed"
  15. set_error_handler(function () {}, E_NOTICE);
  16. // convert input to utf-8 and strip invalid characters
  17. $value = iconv($encoding, 'UTF-8//IGNORE', $input);
  18. // stop silencing of notices
  19. restore_error_handler();
  20. // return input if something went wrong, maybe it's usable anyway
  21. if ($value === false) {
  22. return $input;
  23. }
  24. return $value;
  25. }
  26. }