php中文字符转十六进制的方法:1、创建一个PHP示例文件;2、通过“function hexencode($s){return preg_replace('/(.)/es',"str_pad(...)}”方法将汉字转换为16进制编码即可。
本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php中文字符怎么转十六进制?
php中文汉字与16进制编码转换三种方法
用php实现中文汉字与16进制间的编码转换。
例1,
<?php //汉字转换为16进制编码 function hexencode($s) { return preg_replace('/(.)/es',"str_pad(dechex(ord('\\1')),2,'0',str_pad_left)",$s); } //16进制编码转换为汉字 function hexdecode($s) { return preg_replace('/(\w{2})/e',"chr(hexdec('\\1'))",$s); } echo hexdecode(hexencode("北京欢迎您!")); ?>
方法2,
echo rawurlencode("北京欢迎您").'<br>';
返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数。
解码用:rawurldecode
方法3,
gbk版:
gbk编码中一个汉字由二个字符组成,
通过ord()函数获取字符的ascii值如果大于127时,就可以确定当前字符为一个汉字的前半部分,还需要获取汉字的后半部分。
当然,这种判断的方法要结合具体的开发环境,如果存在ascii值大于127的单个字符,这种方法判断显然就不正确。
php实现中文字符转十进制的原理就是通过for循环的方法获取一个汉字的二个字符,然后使用ord()函数把各字符转换为十进制。
如上分别是:不 [178 187] 要 [210 170] 迷 [195 212] 恋 [193 181] 哥 [184 231]
php 实现中文字符转十六进制的原理:
首先使用ord()函数取出各个中文字符的十进制,具体可查看[php函数篇掌握ord()与chr()函数应用],然后使用dechex()函数把各个中文字符转化为十六进制。
代码:
<?php $string = "北京欢迎您!"; $length = strlen($string); echo $string; $result = array(); //十进制 for($i=0;$i<$length;$i++){ if(ord($string[$i])>127){ $result[] = ord($string[$i]).' '.ord($string[++$i]); } } var_dump($result); echo '<br>'; //十六进制 $strings = array(); foreach($result as $v){ $dec = explode(" ",$v); $strings[] = dechex($dec[0])." ".dechex($dec[1]); } var_dump($strings); utf-8版: $string = "北京欢迎您!"; $length = strlen($string); echo $string; $result = array(); //十进制 for($i=0;$i<$length;$i++){ if(ord($string[$i])>127){ $result[] = ord($string[$i]).' '.ord($string[++$i]).' '.ord($string[++$i]); } } var_dump($result); echo '<br>'; //十六进制 $strings = array(); foreach($result as $v){ $dec = explode(" ",$v); $strings[] = dechex($dec[0])." ".dechex($dec[1])." ".dechex($dec[2]); } var_dump($strings);
推荐学习:《PHP视频教程》
The above is the detailed content of How to convert Chinese characters in php to hexadecimal. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
