Home > Article > Backend Development > What to do if php htmlentities is garbled
php Solution to garbled htmlentities: 1. Use "htmlentities($str,ENT_COMPAT,"GB2312")"; 2. Output "htmlspecialchars($str)".
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
php htmlentities What should I do if the code is garbled?
htmlentities in php causes the problem that Chinese cannot be queried
htmlspecialchars in php converts special characters into HTML format, while htmlentities converts all characters into HTML characters Strung together, let me briefly introduce them separately.
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) is converted to &
" (double quotation marks) is converted to "
c0d94ebc273a150b5cb6e303b39c3c06 (greater than) is converted to >
Example
$str = "John & 'Adams'"; echo htmlspecialchars($str, ENT_COMPAT); echo " "; echo htmlspecialchars($str, ENT_QUOTES); echo " "; echo htmlspecialchars($str, ENT_NOQUOTES); ?>
Their difference
The functions of these two functions are to convert characters into HTML character encoding, especially is the url and code string. Prevent character tags from being executed by the browser. There is no difference when using Chinese, but htmlentities will format Chinese characters so that Chinese input is garbled
htmlentities converts all html tags, htmlspecialchars only formats the special symbols & ' " 709755eb0d440af414163d2e5288b1b1
Effect:
$str = '<a href="demo.php?m=index&a=index&name=中文" rel="external nofollow" rel="external nofollow" >测试页面</a>';
echo 'htmlentities specifies GB2312 encoding: '.htmlentities($str,ENT_COMPAT,"GB2312").'';
echo 'htmlentities does not specify encoding: '.htmlentities($str).'';
$str = '<a href="demo.php?m=index&a=index&name=中文" rel="external nofollow" rel="external nofollow" >测试页面</a>';
echo htmlspecialchars($str).'';
Recommended learning: "PHP Video Tutorial 》
The above is the detailed content of What to do if php htmlentities is garbled. For more information, please follow other related articles on the PHP Chinese website!