Resolution of the conversion problem of HTML tags in PHP

Source: Internet
Author: User

When writing PHP , many friends will inevitably encounter the need to escape the HTML tag storage. such as database, XML files. When it is stored, it needs to be translated into HTML output . There are many conversion functions written by people on the Internet, which are very long and difficult to understand. In fact, PHP has already come with such a function. You don't have to write it yourself.

The following describes the two functions respectively.

1.htmlentities () function:

Description: Converts HTML tags to special characters. For example, convert <script> to "&lt;script&gt;"

Example:

[PHP]View Plaincopy
  1. An imaginary article submission from a bad user
  2. It would redirect anyone to example.com if the code was run in a browser
  3. $userInput = "I am going to hax0r your site, hahaha!
  4. <script type=&apos;text/javascript&apos;>
  5. Window.location = &apos;http://www.example.com/&apos;
  6. </script>&apos; ";
  7. Lets make it safer before we use it
  8. $userInputEntities = Htmlentities ($userInput);
  9. Now we can display it
  10. Echo $userInputEntities;

Since the most recent CSDN control is garbage, change the above $apos to single quotes. ---call!

When the above statement executes, the following results are generated

[HTML]View Plaincopy
    1. I am going to hax0r your site, hahaha!
    2. <script type=&apos;text/javascript&apos;>
    3. Window.location = &apos;http://www.88web.org/&apos;
    4. </script>&apos;
2.html_entity_decode () function

Description: Converts a string that is escaped from the htmlentities () function to an HTML tag.

Example:

[PHP]View Plaincopy
    1. $orig = "I&apos;ll/" walk/"the <b>dog</b> Now";
    2. $a = htmlentities ($orig);
    3. $b = Html_entity_decode ($a);
    4. echo $a; I'll "Walk" the <b>dog</b> Now
    5. Echo $b; I'll "Walk" the <b>dog</b> Now

Reprinted from: http://blog.sina.com.cn/s/blog_a144530d0101d6li.html

Resolution of the conversion problem of HTML tags in PHP

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.