code: public static function toUtf8($str)
      {
          if (\extension_loaded('mbstring') && \function_exists('iconv')) {
              $encoding = \mb_detect_encoding($str, \mb_detect_order(), true);
              if (!$encoding) {
                  $str_conv = false;
                  if (\function_exists('iconv')) {
                      $str_conv = \iconv('cp1252', 'UTF-8', $str);
                  }
                  if ($str_conv === false) {
                      $str_conv = \htmlentities($str, ENT_COMPAT);
                      $str_conv = \html_entity_decode($str_conv, ENT_COMPAT, 'UTF-8');
                  }
                  $str = $str_conv;
              } elseif (!\in_array($encoding, array('ASCII','UTF-8'))) {
                  $str_new = \iconv($encoding, 'UTF-8', $str);
                  if ($str_new !== false) {
                      $str = $str_new;
                  }
              }
          }
          return $str;
      }
docs: Attempt to convert string to UTF-8 encoding
  
  @param string $str string to convert
  
  @return string