Php 操纵美元和美元的交易价值;分
让我们限制值用户可以输入+使用函数正确打印结果Php 操纵美元和美元的交易价值;分,php,Php,让我们限制值用户可以输入+使用函数正确打印结果 <input name="dollar"... <input name="cent"... <?PHP if ( !is_numeric($_POST['dollar'])) { return; } if ( !is_numeric($_POST['cent'])) { return; } $total = $_POST['dollar'].".".$_POST['cent']; 您可以使用一个名为的函数。在这种情况下,解
<input name="dollar"...
<input name="cent"...
<?PHP
if ( !is_numeric($_POST['dollar'])) { return; }
if ( !is_numeric($_POST['cent'])) { return; }
$total = $_POST['dollar'].".".$_POST['cent'];
您可以使用一个名为的函数。在这种情况下,解决方案是:
<input name="dollar"...
<input name="cent"...
<?php
//Dollar is an integer above zero
$dollar = max(0, intval($_POST['dollar']));
//Cent is an integer between 0 and 99
$cent = min(99, max(0, intval($_POST['cent'])));
//Version to place into logs
$total = $dollar.".".$cent;
// Added from another answer to keep everything together
//Set the local to parse monetary values correctly
setlocale(LC_MONETARY,"en_US");
//Get correct monetary value with 2 decimal place and **$** sign
//Version to print
$total = money_format("%.2n", $total);
或者更好:
$total = money_format($_POST['dollar'].".".$_POST['cent']);
希望有帮助。
祝你好运 为什么用户不能在2030年为$u POST['dollar']和$u POST['cent']分别输入6和6?我的日志会显示2030.06而不是2030.6不必担心返回,它只是停止脚本并将错误消息返回到视图看起来像是一项工作,或者可能是我现在所做的工作的副本是乘以100--->203.06*100
$dollar = filter_input(INPUT_POST, "dollar");
$cent = filter_input(INPUT_POST, "cent");
//Set the local to parse monetary values correctly
setlocale(LC_MONETARY,"en_US");
//Get correct monetary value with 2 decimal place and **$** sign
$total = money_format("%.2n", $dollar.'.'.$cent);