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 1088219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:02:38+00:00 2026-05-16T23:02:38+00:00

I’m looking for a general strategy/advice on how to handle invalid UTF-8 input from

  • 0

I’m looking for a general strategy/advice on how to handle invalid UTF-8 input from users.

Even though my web application uses UTF-8, somehow some users enter invalid characters. This causes errors in PHP’s json_encode() and overall seems like a bad idea to have around.

W3C I18N FAQ: Multilingual Forms says "If non-UTF-8 data is received, an error message should be sent back.".

  • How exactly should this be practically done, throughout a site with dozens of different places where data can be input?
  • How do you present the error in a helpful way to the user?
  • How do you temporarily store and display bad form data so the user doesn’t lose all their text? Strip bad characters? Use a replacement character, and how?
  • For existing data in the database, when invalid UTF-8 data is detected, should I try to convert it and save it back (how? utf8_encode()? mb_convert_encoding()?), or leave as-is in the database but doing something (what?) before json_encode()?

I’m very familiar with the mbstring extension and am not asking "how does UTF-8 work in PHP?". I’d like advice from people with experience in real-world situations how they’ve handled this.

As part of the solution, I’d really like to see a fast method to convert invalid characters to U+FFFD.

  • 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-05-16T23:02:39+00:00Added an answer on May 16, 2026 at 11:02 pm

    The accept-charset="UTF-8" attribute is only a guideline for browsers to follow, and they are not forced to submit that in that way. Crappy form submission bots are a good example…

    I usually ignore bad characters, either via iconv() or with the less reliable utf8_encode() / utf8_decode() functions. If you use iconv, you also have the option to transliterate bad characters.

    Here is an example using iconv():

    $str_ignore = iconv('UTF-8', 'UTF-8//IGNORE', $str);
    $str_translit = iconv('UTF-8', 'UTF-8//TRANSLIT', $str);
    

    If you want to display an error message to your users I’d probably do this in a global way instead of a per value received basis. Something like this would probably do just fine:

    function utf8_clean($str)
    {
        return iconv('UTF-8', 'UTF-8//IGNORE', $str);
    }
    
    $clean_GET = array_map('utf8_clean', $_GET);
    
    if (serialize($_GET) != serialize($clean_GET))
    {
        $_GET = $clean_GET;
        $error_msg = 'Your data is not valid UTF-8 and has been stripped.';
    }
    
    // $_GET is clean!
    

    You may also want to normalize new lines and strip (non-)visible control chars, like this:

    function Clean($string, $control = true)
    {
        $string = iconv('UTF-8', 'UTF-8//IGNORE', $string);
    
        if ($control === true)
        {
                return preg_replace('~\p{C}+~u', '', $string);
        }
    
        return preg_replace(array('~\r\n?~', '~[^\P{C}\t\n]+~u'), array("\n", ''), $string);
    }
    

    Code to convert from UTF-8 to Unicode code points:

    function Codepoint($char)
    {
        $result = null;
        $codepoint = unpack('N', iconv('UTF-8', 'UCS-4BE', $char));
    
        if (is_array($codepoint) && array_key_exists(1, $codepoint))
        {
            $result = sprintf('U+%04X', $codepoint[1]);
        }
    
        return $result;
    }
    
    echo Codepoint('à'); // U+00E0
    echo Codepoint('ひ'); // U+3072
    

    It is probably faster than any other alternative, but I haven’t tested it extensively though.


    Example:

    $string = 'hello world�';
    
    // U+FFFEhello worldU+FFFD
    echo preg_replace_callback('/[\p{So}\p{Cf}\p{Co}\p{Cs}\p{Cn}]/u', 'Bad_Codepoint', $string);
    
    function Bad_Codepoint($string)
    {
        $result = array();
    
        foreach ((array) $string as $char)
        {
            $codepoint = unpack('N', iconv('UTF-8', 'UCS-4BE', $char));
    
            if (is_array($codepoint) && array_key_exists(1, $codepoint))
            {
                $result[] = sprintf('U+%04X', $codepoint[1]);
            }
        }
    
        return implode('', $result);
    }
    

    This may be what you were looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
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
Does anyone know how can I replace this 2 symbol below from the string

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.