HTML表单
<form action="createpdf.php" method="POST">
<input type="text" name="title[]" />
<input type="text" name="unit_price[]" />
<input type="text" name="item_quantity[]" />
<input type="text" name="total_cost[]" />
// etc...
</form>现在将插入的值发送到createpdf.php
将转换为PDF / createpdf.php的TCPDF PHP代码
$html = 'something...';
foreach($_POST['title'] as $key => $v1){
$html = $html.'
<tr>
<td>'.$_POST['item_title'][$key].'</td>
<td>'.$_POST['unit_price'][$key].'</td>
<td>'.$_POST['item_quantity'][$key].'</td>
<td>'.$_POST['total_cost'][$key].'</td>
</tr>
';}
$pdf->writeHTML($html, true, false, true, false, '');目标
有谁能告诉我如何使用以下函数,在POSTing数据前将值格式化为表格单元格?我需要这个模式x xxx.dd只是不知道怎么..。非常感谢
function format_price($number,$decPlaces,$decSep,$thouSep){
//$number - number for format
//$decPlaces - number of decimal places
//$decSep - separator for decimals
//$thouSep - separator for thousands
//first remove all white spaces
$number=preg_replace('/\s+/', '',$number);
//split string into array
$numberArr = str_split($number);
//reverse array and not preserve key, keys will help to find decimal place
$numberArrRev=array_reverse($numberArr);
//find first occurrence of non number character, that will be a decimal place
//store $key into variable $decPointIsHere
foreach ($numberArrRev as $key => $value) {
if(!is_numeric($value)){
if($decPointIsHere==""){
$decPointIsHere=$key;
}
}
}
//decimal comma or whatever it is replace with dot
//$decPointIsHere is the key of the element that will contain decimal separator dot
if($decPointIsHere!=""){
$numberArrRev[$decPointIsHere]=".";
}
//again check through array for non numerical characters but skipping allready processed keys
//if is not number remove from array
foreach ($numberArrRev as $key => $value) {
if(!is_numeric($value) && $key>$decPointIsHere){
unset($numberArrRev[$key]);
}
}
//reverse back, at the start reversed array $numberArrRev to $numberArr
$numberArr=array_reverse($numberArrRev);
//create string from array
$numberClean=implode("",$numberArr);
// apply php number_format function
return number_format($numberClean,$decPlaces,$decSep,$thouSep);
}发布于 2014-03-16 18:12:48
试试这个:
<td>'.number_format($_POST['unit_price'][$key], 2, ".", " ").'</td>发布于 2014-03-16 18:13:20
根据你的描述,我认为这应该是:
<td>' . format_price($_POST['unit_price'][$key],2,'.','') . '</td>发布于 2014-03-16 18:13:38
评论告诉你如何做到这一点:
format_price($_POST['unit_price'][$key], 2, ".", " ");https://stackoverflow.com/questions/22440631
复制相似问题