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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:10:59+00:00 2026-05-26T22:10:59+00:00

According to a function here, http://www.unicodetools.com/unicode/convert-to-html.php , the function is used to convert string

  • 0

According to a function here, http://www.unicodetools.com/unicode/convert-to-html.php, the function is used to convert string to HTML encoded text.

The JavaScript is:

function a(b) {
    var c= '';

    for(i=0; i<b.length; i++) {
        if(b.charCodeAt(i)>127) {
            c += '&#' + b.charCodeAt(i) + ';'; 
        } else { 
            c += b.charAt(i); 
        }
  }

  document.forms.conversionForm.outputText.value = c;
}

And my try is:

function str_to_html_entity($str) {
    $output = NULL;

    for($i = 0; $i < strlen($str); $i++) {
        if(ord($str) > 127) {
            $output .= '&#' + ord($str) + ';'; 
        } else { 
            $output .= substr($str, $i); 
        }
  }

  return $output;
}

echo str_to_html_entity("Thére Àre sôme spëcial charâcters ïn thìs têxt");

My PHP function run correctly, but the result is not what I expected:

my result:

Thére Àre sôme spëcial charâcters ïn thìs têxthére Àre sôme spëcial charâcters ïn thìs têxtére Àre sôme spëcial charâcters ïn thìs têxt�re Àre sôme spëcial charâcters ïn thìs têxtre Àre sôme spëcial charâcters ïn thìs têxte Àre sôme spëcial charâcters ïn thìs têxt Àre sôme spëcial charâcters ïn thìs têxtÀre sôme spëcial charâcters ïn thìs têxt�re sôme spëcial charâcters ïn thìs têxtre sôme spëcial charâcters ïn thìs têxte sôme spëcial charâcters ïn thìs têxt sôme spëcial charâcters ïn thìs têxtsôme spëcial charâcters ïn thìs têxtôme spëcial charâcters ïn thìs têxt�me spëcial charâcters ïn thìs têxtme spëcial charâcters ïn thìs têxte spëcial charâcters ïn thìs têxt spëcial charâcters ïn thìs têxtspëcial charâcters ïn thìs têxtpëcial charâcters ïn thìs têxtëcial charâcters ïn thìs têxt�cial charâcters ïn thìs têxtcial charâcters ïn thìs têxtial charâcters ïn thìs têxtal charâcters ïn thìs têxtl charâcters ïn thìs têxt charâcters ïn thìs têxtcharâcters ïn thìs têxtharâcters ïn thìs têxtarâcters ïn thìs têxtrâcters ïn thìs têxtâcters ïn thìs têxt�cters ïn thìs têxtcters ïn thìs têxtters ïn thìs têxters ïn thìs têxtrs ïn thìs têxts ïn thìs têxt ïn thìs têxtïn thìs têxt�n thìs têxtn thìs têxt thìs têxtthìs têxthìs têxtìs têxt�s têxts têxt têxttêxtêxt�xtxtt

expected result:

Th&#233;re &#192;re s&#244;me sp&#235;cial char&#226;cters &#239;n th&#236;s t&#234;xt

Could someone please advise what wrong with my PHP function?

Thanks

UPDATE

function str_to_html_entity($str) {
    $result = null;
    for ($i = 0, $length = mb_strlen($str, 'UTF-8'); $i < $length; $i++) {
        $character = mb_substr($str, $i, 1, 'UTF-8');
        if (strlen($character) > 1) {  // the character consists of more than 1 byte
            $character = htmlentities($character, ENT_COMPAT, 'UTF-8');
        }
        $result .= $character;
    }

  return $result;
}

echo str_to_html_entity("Thére Àre"); // Th&eacute;re &Agrave;re
echo str_to_html_entity("中"); // 中
  • 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-26T22:11:00+00:00Added an answer on May 26, 2026 at 10:11 pm

    Generally:

    • Javascript strings are Unicode aware, which means str[0] will return one character, however long this character is. charCodeAt will correctly return character codes for any character.
    • PHP strings are dumb binary arrays, in which a character may take up more than one byte. $str[0] and ord only work on single bytes and will therefore mangle any multi-byte characters. See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text for an in-depth explanation of this.

    Because of this, you can’t replicate the exact same algorithm in PHP. Also, in your loop, you’re using the whole $str instead of a string offset, which is your other primary problem. To make it Unicode aware, this is probably the nicest way:

    $result = null;
    foreach (preg_split('/./u', $str) as $character) {
        if (strlen($character) > 1) {  // the character consists of more than 1 byte
            $character = mb_convert_encoding($character, 'HTML-ENTITIES', 'UTF-8');
        }
        $result .= $character;
    }
    

    This expects the string to be UTF-8 encoded. As you can see though, there’s a nice function called mb_convert_encoding, which can escape a whole block of text in one go, which you’re essentially reinventing. Use it instead.

    Alternative version for Unicode-impaired PCREs:

    $result = null;
    for ($i = 0, $length = mb_strlen($str, 'UTF-8'); $i < $length; $i++) {
        $character = mb_substr($str, $i, 1, 'UTF-8');
        if (strlen($character) > 1) {  // the character consists of more than 1 byte
            $character = mb_convert_encoding($character, 'HTML-ENTITIES', 'UTF-8');
        }
        $result .= $character;
    }
    

    But seriously, just use $str = mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8') and be done with it. No looping required.

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

Sidebar

Related Questions

According to http://www.codeguru.com/forum/showthread.php?t=463663 , C#'s getHashCode function in 3.5 is implemented as: public override
So according to the link here: http://www.cplusplus.com/reference/algorithm/max_element/ , the max_element function is O(n), apparently
I am trying to use the jQuery CSV plugin, as documented here: http://code.google.com/p/js-tables/wiki/CSV According
I am using jQuery UI for the sidebar navigation here: http://www.imadesign.com/dev/work/ I would like
Here is my site: http://www.raceramps.com/v2 Move your mouse over the menu on the right
According to http://www.cplusplus.com/reference/clibrary/ctime/time_t/ time_t is the number of seconds since midnight 1/1/1970 UTC. So
I am using following a tutorial from here: http://www.shopdev.co.uk/blog/cookies-with-jquery-designing-collapsible-layouts/ This is the script I
I was reading up on this javascript tutorial: http://www.switchonthecode.com/tutor...ccordion-menus Basically, it shows you how
This article explains how to use Zend in Codeigniter. http://www.beyondcoding.com/2008/02/21/using-zend-framework-with-codeigniter/ I am using XAMPP
I want to make drop-down menu with jquery (here's the according jsFiddle ). HTML:

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.