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

What to do if php htmlentities is garbled

藏色散人
Release: 2023-03-14 14:56:01
Original
1655 people have browsed it

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'
Copy after login

Browser output:

htmlspecialchars usage

& (and) is converted to &
" (double quotation marks) is converted to "
< (less than) is converted to <
> (greater than) is converted to >

Example

$str = "John & &#39;Adams&#39;"; 
 echo htmlspecialchars($str, ENT_COMPAT); 
 echo " 
 "; 
 echo htmlspecialchars($str, ENT_QUOTES); 
 echo " 
 "; 
 echo htmlspecialchars($str, ENT_NOQUOTES); 
 ?>
Copy after login

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 & ' " < and >

Effect:

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

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;;
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template