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:
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 );
?>
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...