Htmlspecialchars and htmlentities usage in php _ PHP Tutorial

Source: Internet
Author: User
Htmlspecialchars and htmlentities usage in php. In php, htmlspecialchars converts special characters into HTML format, while htmlentities converts all the elements into HTML strings. next I will briefly introduce them. Htmlentities usage htmlspecialchars in php converts special characters into HTML format, while htmlentities converts all the elements into HTML strings. next I will give a brief introduction to them.

Htmlentities usage



$ Str = "John & 'Adams '";
Echo htmlentities ($ str, ENT_COMPAT );
Echo"
";
Echo htmlentities ($ str, ENT_QUOTES );
Echo"
";
Echo htmlentities ($ str, ENT_NOQUOTES );
?>

John & 'Adams'
John & 'Adams'
John & 'Adams'

Browser output:
The code is as follows:

Htmlspecialchars usage

& (And) &
"(Double quotation marks)"
<(Less than) to <
> (Greater than) to>

Example



$ Str = "John & 'Adams '";
Echo htmlspecialchars ($ str, ENT_COMPAT );
Echo"
";
Echo htmlspecialchars ($ str, ENT_QUOTES );
Echo"
";
Echo htmlspecialchars ($ str, ENT_NOQUOTES );
?>

The code is as follows:

Their differences

Both functions convert characters into HTML characters, especially url and code strings. Prevents the character Mark from being executed by the browser. There is no difference when using Chinese, but htmlentities will format Chinese characters to make Chinese input garbled

Htmlentities converts all html tags. htmlspecialchars only formats the special symbols & '"<and>.

The code is as follows:

$ Str = 'test page ';

Echo 'htmlentities specify GB2312 encoding: '.html entities ($ str, ENT_COMPAT, "GB2312 ").'';

Echo 'htmlentities unspecified encoding: '.html entities ($ str ).'';

$ Str = 'test page ';

Echo htmlspecialchars ($ str ).'';

Effect:

The code is as follows:

Htmlentities specify GB2312 encoding: Test page

Htmlentities does not specify the encoding: ² â ~~~~~~~~

Test page


Characters, convert special characters to HTML format, and htmlentities convert all the elements into HTML strings. next I will give a brief introduction to them. Htmlentities usage...

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.