Home >Backend Development >PHP Problem >How to convert Chinese characters to location code in php

How to convert Chinese characters to location code in php

藏色散人
藏色散人Original
2020-10-05 09:34:082339browse

php method to convert Chinese characters to location code: first create a PHP sample file; then pass "sprintf(" d d",ord($t1[0])-160,ord($t1[1 ])-160);" method can be used to convert Chinese characters into location codes.

How to convert Chinese characters to location code in php

Recommended: "PHP Video Tutorial"

Analysis of source code application examples for converting Chinese characters to location codes in PHP

The problem of how to convert Chinese characters to location codes in PHP has always troubled most programmers, so I believe the following source code example will be of great help to everyone.

The code is as follows:

<?php 
global $PHP_SELF; 
//echo $PHP_SELF; 
$t1=$_POST[&#39;textfield1&#39;]; 
$t2=$_POST[&#39;textfield2&#39;]; 
$t3=$_POST[&#39;textfield3&#39;]; 
$t4=$_POST[&#39;textfield4&#39;]; 
// 汉字--区位码 
if($t1!=""){ 
$t2= sprintf("%02d%02d",ord($t1[0])-160,ord($t1[1])-160); 
//echo $t2; 
} 
// 区位码--汉字 
if($t3!=""){ 
$t4 = chr(substr($t3,0,2)+160).chr(substr($t3,2,2)+160); 
//echo $t4; 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>无标题文档</title> 
<style type="text/css"> 
<!-- 
.STYLE1 {font-size: 18px} 
--> 
</style> 
</head> 
<body> 
<table width="528" height="146" border="1" 
align="center" cellpadding="0" cellspacing="0"> 
<tr> 
<td width="524" height="50"><div align="center" 
class="STYLE1">汉字区位码查询系统</div></td> 
</tr> 
<tr> 
<td><form id="form1" name="form1" method="post" action= 
"<?=$PHP_SELF ?>"> 
<label>输入汉字 
<input name="textfield1" type="text" value="<?=$t1?>" /> 
</label> 
<label> 
<input type="submit" name="Submit" value=" 转 换 " /> 
</label> 
<label> 
<input name="textfield2" type="text" value="<?=$t2?>" /> 
</label> 
</form> 
<br /> 
<form id="form2" name="form2" method="post" action="<?=$PHP_SELF ?>"> 
<label>输入区位码 
<input name="textfield3" type="text" value="<?=$t3?>" /> 
</label> 
<input type="submit" name="Submit2" value=" 转 换 " /> 
<input name="textfield4" type="text" value="<?=$t4?>" /> 
</form> 
</td> 
</tr> 
</table> 
</body> 
</html>

The above is the detailed content of How to convert Chinese characters to location code in php. 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