Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7868851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:07:51+00:00 2026-06-03T01:07:51+00:00

I’ve been using a couple of methods on a simple class, which have worked

  • 0

I’ve been using a couple of methods on a simple class, which have worked ok for me, but I’ve noticed that they are really slow due to strtr() and having a ton of translations defined. Also it is really long and thus, more difficult to maintain and understand.

That said, all of the “bad” examples are solutions to real-world issues which have come up, translating strings into UTF8.

Can anyone tell me of there’s a well known or more efficient means of doing this? (yes, I’ve tried the htmlentities() methods and the iconv()methods but neither really replaced all the funky characters correctly.

Here’s the class I’m using currently:
https://gist.github.com/2559140

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:07:53+00:00Added an answer on June 3, 2026 at 1:07 am

    mbstring support is now enabled (but not loaded) by default as of PHP 5.4.0. Loading the extension, this lets you do:

    <? //PHP 5.4+
    $ensureIsUTF8 = static function($data){
        $dataEncoding = \mb_detect_encoding(
            $data,
            ['UTF-8', 'windows-1251', 'iso-8859-1', /*others you encounter*/],
            true
        );
    
        //UTF-16/32 encoding detection always fails for PHP <= 5.4.1
        //Use detection code copied from PHP docs comments:
        //http://www.php.net/manual/en/function.mb-detect-encoding.php
        if ($dataEncoding === false){
    
            $UTF32_BIG_ENDIAN_BOM = chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF);
            $UTF32_LITTLE_ENDIAN_BOM = chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00);
            $UTF16_BIG_ENDIAN_BOM = chr(0xFE) . chr(0xFF);
            $UTF16_LITTLE_ENDIAN_BOM = chr(0xFF) . chr(0xFE);
    
            $first2 = \substr($data, 0, 2);
            $first4 = \substr($data, 0, 4);
    
            if ($first4 === $UTF32_BIG_ENDIAN_BOM) {
                $dataEncoding = 'UTF-32BE';
            } elseif ($first4 === $UTF32_LITTLE_ENDIAN_BOM) {
                $dataEncoding = 'UTF-32LE';
            } elseif ($first2 === $UTF16_BIG_ENDIAN_BOM) {
                $dataEncoding = 'UTF-16BE';
            } elseif ($first2 === $UTF16_LITTLE_ENDIAN_BOM) {
                $dataEncoding = 'UTF-16LE';
            } else {
                throw new \Exception('Whoa! No idea what that was.');
            }
        }
    
        if ($dataEncoding === 'UTF-8'){
            return $data;
        } else {
            return \mb_convert_encoding(
               $data,
               'UTF-8',
               $dataEncoding
            );      
        }
    };
    
    $utf8Data = $ensureIsUTF8(\file_get_contents('something'));
    $utf8Data = $ensureIsUTF8(\file_get_contents('http://somethingElse'));
    $utf8Data = $ensureIsUTF8($userProvidedData);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.