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

  • SEARCH
  • Home
  • 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 8672233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:09:07+00:00 2026-06-12T19:09:07+00:00

Possible Duplicate: How to convert text to unicode code point like \u0054\u0068\u0069\u0073 using php?

  • 0

Possible Duplicate:
How to convert text to unicode code point like \u0054\u0068\u0069\u0073 using php?

I’m trying to convert all characters that can’t fit into a 7-bit ANSI character into an escaped form, \uN, where N is its decimal value. Here’s what I’ve come up with:

private static function escape($str) {
    return preg_replace_callback('~[\\x{007F}-\\x{FFFF}]~u',function($m){return '\\u'.ord($m[0]);},$str);
}

I’ve tried it with characters like Gamma,

echo self::escape('Γ');

But I get \u206 back out instead of \u915. I can’t figure out where I’m going wrong… ideas?

Actually, it appears that either the ord() function doesn’t give me the value or I want, or maybe the encoding on my .php file is wrong?

  • 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-12T19:09:08+00:00Added an answer on June 12, 2026 at 7:09 pm

    I had to refresh my memory on exactly how UTF-8 works, but here is a utf8_ord() function, and a complementing utf8_chr(). The chr() is lifted pretty much verbatim from my answer here.

    function utf8_ord ($chr)
    {
        $bytes = array_values(unpack('C*', $chr));
    
        switch (count($bytes)) {
            case 1:
                return $bytes[0] < 0x80
                    ? $bytes[0]
                    : false;
            case 2:
                return ($bytes[0] & 0xE0) === 0xC0 && ($bytes[1] & 0xC0) === 0x80
                    ? (($bytes[0] & 0x1F) << 6) | ($bytes[1] & 0x3F)
                    : false;
            case 3:
                return ($bytes[0] & 0xF0) === 0xE0 && ($bytes[1] & 0xC0) === 0x80 && ($bytes[2] & 0xC0) === 0x80 
                    ? (($bytes[0] & 0x0F) << 12) | (($bytes[1] & 0x3F) << 6) | ($bytes[2] & 0x3F)
                    : false;
            case 4:
                return ($bytes[0] & 0xF8) === 0xF0 && ($bytes[1] & 0xC0) === 0x80 && ($bytes[2] & 0xC0) === 0x80 && ($bytes[3] & 0xC0) === 0x80
                    ? (($bytes[0] & 0x07) << 18) | (($bytes[1] & 0x3F) << 12) | (($bytes[2] & 0x3F) << 6) | ($bytes[3] & 0x3F)
                    : false;
        }
    
        return false;
    }
    
    function utf8_chr ($ord)
    {
        switch (true) {
            case $ord < 0x80:
                return pack('C*', $ord & 0x7F);
            case $ord < 0x0800:
                return pack('C*', (($ord & 0x07C0) >> 6) | 0xC0, ($ord & 0x3F) | 0x80);
            case $ord < 0x010000:
                return pack('C*', (($ord & 0xF000) >> 12) | 0xE0, (($ord & 0x0FC0) >> 6) | 0x80, ($ord & 0x3F) | 0x80);
            case $ord < 0x110000:
                return pack('C*', (($ord & 0x1C0000) >> 18) | 0xF0, (($ord & 0x03F000) >> 12) | 0x80, (($ord & 0x0FC0) >> 6) | 0x80, ($ord & 0x3F) | 0x80);
        }
    
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to convert yyyy-MM-dd HH:mm:ss to “15th Apr 2010” using PHP I
Possible Duplicate of Convert jpg image to gif, png & bmp format using PHP
Possible Duplicate: Convert HTML + CSS to PDF with PHP? Please give code for
Possible Duplicate: How can I convert ereg expressions to preg in PHP? I need
Possible Duplicate: Convert HTML to PDF in .NET I am using VSTS 2008 +
Possible Duplicate: Interpreting newlines with XSLT xsl:text? How to convert newline into <br/> with
Possible Duplicate: Looking for C# equivalent of scanf I am trying to convert a
Possible Duplicate: How to decode HTML entities using jQuery? I want to convert this
Possible Duplicate: Convert one date format into another in PHP I know there are
Possible Duplicate: Convert string to DateTime in c# I am trying to convert a

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.