If you have encountered any problems, please refer to the php Tutorial.

Source: Internet
Author: User
I have encountered some problems in learning. please come over and give some advice. When I look at the framework source code (for example: ci), I am not familiar with the writing. what should I do if I have no guidance? The following code is used: when strtolower ($ charset )! UTF-8 is a processing method, not a processing method. why? It may take half a day to solve a problem like this if you go to the forum to consult with someone else. if you search for a problem like this, you cannot start it. In distress... what should I do? If you have encountered any problems, please contact us.
When looking at the framework source code (for example, ci)
What should I do if I don't know how to write it?
The following code is used: when strtolower ($ charset )! = 'Utf-8' is a processing method, not a processing method. why? It may take half a day to solve a problem like this if you go to the forum to consult with someone else. if you search for a problem like this, you cannot start it. In distress... what should I do? I hope you will be grateful for your guidance.
PHP code
      public function entity_decode($str, $charset='UTF-8')    {        if (stristr($str, '&') === FALSE) return $str;        // The reason we are not using html_entity_decode() by itself is because        // while it is not technically correct to leave out the semicolon        // at the end of an entity most browsers will still interpret the entity        // correctly.  html_entity_decode() does not convert entities without        // semicolons, so we are left with our own little solution here. Bummer.        if (function_exists('html_entity_decode') &&            (strtolower($charset) != 'utf-8'))        {            $str = html_entity_decode($str, ENT_COMPAT, $charset);            $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);            return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);        }        // Numeric Entities        $str = preg_replace('~&#x(0*[0-9a-f]{2,5});{0,1}~ei', 'chr(hexdec("\\1"))', $str);        $str = preg_replace('~&#([0-9]{2,4});{0,1}~e', 'chr(\\1)', $str);        // Literal Entities - Slightly slow so we do another check        if (stristr($str, '&') === FALSE)        {            $str = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES)));        }        return $str;    }


------ Solution --------------------
... I don't know much about it either. You can click it to see it.
------ Solution --------------------
OK. first, this function is defective,

Try:
Echo entity_decode ('Leaf ', 'utf-8 ');
Echo "\ n ";
Echo html_entity_decode ('Leaf ', ENT_COMPAT, 'utf-8 ');

Ye is the word "ye.

So I think
& (Strtolower ($ charset )! = 'Utf-8 ')
It is better to remove this part from the condition. In other words, you can use html_entity_decode first, and then process the result without a semicolon.

Because you only asked this utf8 question, I believe you have no other questions, so I will not talk too much.

Look at other people's code. you can also look at it in detail,
For example, if you are more concerned about this function, you only need to know that it is a variant of html_entity_decode.





------ Solution --------------------
Addslashes ()
------ Solution --------------------
Commented out:


Public function entity_decode ($ str, $ charset = 'utf-8 ')
{
If (stristr ($ str, '&') = FALSE) return $ str; // if not, return directly

// The reason we are not using html_entity_decode () by itself is because
// While it is not technically correct to leave out the semicolon
// At the end of an entity most browsers will still interpret the entity
// Correctly. html_entity_decode () does not convert entities
// Semicolons, so we are left with our own little solution here. Bummer.
// Why not use html_entity_decode () directly, because html_entity_decode () does not directly convert entities without semicolons.

If (function_exists ('HTML _ entity_decode ')&&
(Strtolower ($ charset )! = 'Utf-8 '))
{

// If it is not utf8;

$ Str = html_entity_decode ($ str, ENT_COMPAT, $ charset); // decodes data based on encoding
$ Str = preg_replace ('~ & # X (0 * [0-9a-f] {2, 5 })~ Ei ', 'chr (hexdec ("\ 1")', $ str); // replace
Return preg_replace ('~ & # ([0-9] {2, 4 })~ E ', 'chr (\ 1)', $ str); // return the result after replacement.


}
// For utf8;
// Replace Numeric Entities with Numeric Entities.
$ Str = preg_replace ('~ & # X (0 * [0-9a-f] {2, 5}); {0, 1 }~ Ei ', 'chr (hexdec ("\ 1")', $ str );
$ Str = preg_replace ('~ & # ([0-9] {2, 4}); {0, 1 }~ E ', 'chr (\ 1)', $ str );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.