Home  >  Article  >  Backend Development  >  What to do if php htmlentities is garbled

What to do if php htmlentities is garbled

藏色散人
藏色散人Original
2021-12-21 10:21:541561browse

php Solution to garbled htmlentities: 1. Use "htmlentities($str,ENT_COMPAT,"GB2312")"; 2. Output "htmlspecialchars($str)".

What to do if php htmlentities is garbled

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 = &#39;<a href="demo.php?m=index&a=index&name=中文" rel="external nofollow" rel="external nofollow" >测试页面</a>&#39;;

echo 'htmlentities specifies GB2312 encoding: '.htmlentities($str,ENT_COMPAT,"GB2312").'';

echo 'htmlentities does not specify encoding: '.htmlentities($str).'';

$str = &#39;<a href="demo.php?m=index&a=index&name=中文" rel="external nofollow" rel="external nofollow" >测试页面</a>&#39;;

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn