本帖最后由 jiangbo123 于 2020-3-19 13:38 编辑
- 才学php,利用php写了一个把数字转换为大写中文金额的页面
- 应该有一些bug,请大佬指正
- 语法有哪些可以改进的,请大佬指正
- php有哪些要点,请大佬指正
- 问题:怎么把php的数据提交到html代码中?请大佬指正
代码如下
<?php
$xiao = "";
$da = "";
if(isset($_POST['sub'])){
$num = $_POST['xiao'];
if(is_numeric($num)){
$xiao = $num;
if(strlen(trim($num,'.')) == strlen($num)){
if(strpos($num,'.')){
$num = trim($num,"0");
if(strlen($num) - strpos($num,'.') > 3){
$da = "小数超出范围";
}else{
$da = change_float($num);
}
}else{
$num = ltrim($num,"0");
$da = change_int($num);
}
}else{
$da = "输入错误";
}
}else{
$da = "输入的不是数字";
}
}else{
$da = "请先输入数字";
}
function change_float($num){
$index = strpos($num,'.');
$len = strlen($num);
$nownum = "";
$lastnum = "";
$nar = false;
for($i = 0; $i < $index; $i++ ){
if($num[$i] == 0 && $i != $len - 1){
if($index - $i - 1 == 4 && $nar || $index - $i - 1 == 8 && $nar){
$nar = false;
}elseif($num[$i+1] == 0){
continue;
}else{
$nownum .= change_num($num[$i]);
continue;
}
}elseif($num[$i] == 0){
continue;
}else{
if($index - $i - 1 > 4 && $index - $i - 1 <= 7 || $index - $i - 1 > 8){
$nar = true;
}
$nownum .= change_num($num[$i]);
}
switch($index - $i -1){
case 1 : $nownum .= "拾";break;
case 2 : $nownum .= "佰";break;
case 3 : $nownum .= "仟";break;
case 4 : $nownum .= "万";break;
case 5 : $nownum .= "拾";break;
case 6 : $nownum .= "佰";break;
case 7 : $nownum .= "仟";break;
case 8 : $nownum .= "亿";break;
case 9 : $nownum .= "拾";break;
case 10: $nownum .= "佰";break;
case 11: $nownum .= "仟";break;
case 12: $nownum .= "万";break;
}
}
for($i = $index + 1; $i < $len; $i++ ){
if($num[$i] == 0){
continue;
}else{
$lastnum .= change_num($num[$i]);
}
switch($i - $index){
case 1 : $lastnum .= "角";break;
case 2 : $lastnum .= "分";break;
}
}
if($nownum){
$sumnum = $nownum."元".$lastnum;
}elseif($lastnum){
$sumnum = $lastnum;
}else{
$sumnum = "零元";
}
return $sumnum;
}
function change_int($num){
$len = strlen($num);
$nownum = "";
$nar = false;
for($i = 0; $i < $len; $i++ ){
if($num[$i] == 0 && $i != $len - 1){
if($len - $i - 1 == 4 && $nar || $len - $i - 1 == 8 && $nar){
$nar = false;
}elseif($num[$i+1] == 0){
continue;
}else{
$nownum .= change_num($num[$i]);
continue;
}
}elseif($num[$i] == 0){
continue;
}else{
if($len - $i - 1 > 4 && $len - $i - 1 <= 7 || $len - $i - 1 > 8){
$nar = true;
}
$nownum .= change_num($num[$i]);
}
switch($len - $i - 1){
case 1 : $nownum .= "拾";break;
case 2 : $nownum .= "佰";break;
case 3 : $nownum .= "仟";break;
case 4 : $nownum .= "万";break;
case 5 : $nownum .= "拾";break;
case 6 : $nownum .= "佰";break;
case 7 : $nownum .= "仟";break;
case 8 : $nownum .= "亿";break;
case 9 : $nownum .= "拾";break;
case 10: $nownum .= "佰";break;
case 11: $nownum .= "仟";break;
case 12: $nownum .= "万";break;
}
}
if($nownum){
$sumnum = $nownum."元";
}else{
$sumnum = "零元";
}
return $sumnum;
}
function change_num($i){
switch($i){
case 1: return '壹';
case 2: return '贰';
case 3: return '叁';
case 4: return '肆';
case 5: return '伍';
case 6: return '陆';
case 7: return '柒';
case 8: return '捌';
case 9: return '玖';
case 0: return '零';
default:return '参数错误';exit;
}
}
?>
<html>
<head>
<title>大小写转换</title>
<style type="text/css">
form{
border: 1px solid #CCCCCC;
border-radius: 5px;
padding: 40px 20px 20px;
width: 400px;
height: 200px;
margin: 300px auto;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
form:hover{
box-shadow: 2px 2px 10px #CCCCCC;
}
form input{
margin: auto;
display: block;
width: 100%;
height: 50px;
margin-bottom: 5px;
font-family: "微软雅黑";
font-size: 20px;
border-radius: 5px;
border: 1px solid #CCCCCC;
}
input[type='submit']{
background-color: #4CAF50;
color: #FFFFFF;
border: none;
border-radius: 5px;
width: 100%;
height: 50px;
margin-top: 30px;
}
input[type='submit']:focus{
background-color: #45a049;
outline: none;
}
</style>
</head>
<body>
<form action="#" method="post">
<input type="text" name="xiao" id="xiao" placeholder="请输入数字" value="<?php echo $xiao?>"/>
<input type="text" name="da" id="da" value="<?php echo $da;?>" readonly/>
<input type="submit" name='sub' value="转换为大写"/>
</form>
</body>
</html>
结果截图
|