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
$ 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: |
Copy code |
|
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: |
Copy code |
|
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: |
Copy code |
$ 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: |
Copy code |
Htmlentities specify GB2312 encoding: test page Htmlentities does not specify the encoding: ² â ~~~~~~~~ Test page |