PHP processing of HTML encoded strings

Source: Internet
Author: User
Today, when I write a PHP script to access the database, I found that the Chinese strings are stored in HTML encoding (for example, the HTML code corresponding to the Chinese characters is #27611 ;), this makes it impossible to use Chinese characters to perform a condition query on this field. Instead, you need to convert it into encoding before performing condition matching. Compile readable characters and HTML characters in PHP

Today, when I write a PHP script to access the database, I found that the Chinese strings are stored in HTML encoding (for example, the HTML code corresponding to the Chinese characters is #27611 ;), this makes it impossible to use Chinese characters to perform a condition query on this field. Instead, you need to convert it into encoding before performing condition matching. Compile readable characters and HTML characters in PHP

When I wrote a PHP script to access the database today, I found that the Chinese strings are stored in HTML encoding (for example, the HTML code corresponding to the Chinese text "Mao" is "Mao "), this makes it impossible to use Chinese characters to perform a condition query on this field. Instead, you need to convert it into encoding before performing condition matching.

UseHtmlentitiesAndHtml_entity_decodeFunction. Write the following query statement,

$ SQL = "select id, depart_name, first_name, last_name, local_name, extension, mobile, title ";
$ SQL. = "from pb_extension e, pb_department d ";
$ SQL. = "where e. depart_id = d. depart_id ";
$ SQL. = "and d. plant = '". $ plant ."'";
$ SQL. = "and (first_name like '%". $ HTTP_POST_VARS ["q"]. "% '";
$ SQL. = "or last_name like '%". $ HTTP_POST_VARS ["q"]. "% '";
$ SQL. = "or local_name like '%". htmlentities ($ HTTP_POST_VARS ["q"]). "% '";
$ SQL. = "or extension like '%". $ HTTP_POST_VARS ["q"]. "% '";
$ SQL. = "or mobile like '%". $ HTTP_POST_VARS ["q"]. "% '";
$ SQL. = "or depart_name like '%". $ HTTP_POST_VARS ["q"]. "% '";
$ SQL. = "or title like '%". $ HTTP_POST_VARS ["q"]. "% '";
$ SQL. = ")";

And execute. This statement can be correctly executed and queried in SQL Analyzer. However, the execution of this script in IE prompts that the query result is zero. Why?

After carefully checking this SQL section and checking the source code in IE, we found the root cause of the problem. The query value is converted to HTML encoding by using the htmlentities function. This encoding contains special characters (such as the example in the first section). This character has another meaning in IE. In IE, "&;"In IE, the output SQL statement is different from the actually executed statement.

To solve this problem, we must try to prevent IE from using "&;"It indicates the "&" symbol. After one query, we found that the following code can solve this problem.

$ Trans = get_html_translation_table (HTML_ENTITIES );
$ Trans = array_flip ($ trans );
$ Encoded = "Hallo & <Frau> & Kr ämer ";
$ Original = strtr ($ encoded, $ trans );
Echo $ original;
?>

Run this code and the output $ original variable value is "Hallo & & Kr ämer ", which is the result I want.

The above are the problems and solutions I encountered at work. I wrote them on my Blog and shared them with my friends who encountered similar problems.

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.