1 |
<?php
|
2 |
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/pamc/gen_a/sw/phplib/string/strfunc_int.inc,v 1.1 2007/06/21 23:34:03 dashley Exp $
|
3 |
//--------------------------------------------------------------------------------
|
4 |
//strfunc_int.inc -- PAMC string functions related to integer formatting.
|
5 |
//Copyright (C) 2007 David T. Ashley
|
6 |
//--------------------------------------------------------------------------------
|
7 |
//This program is free software; you can redistribute it and/or
|
8 |
//modify it under the terms of the GNU General Public License
|
9 |
//as published by the Free Software Foundation; either version 2
|
10 |
//of the License, or (at your option) any later version.
|
11 |
//
|
12 |
//This program is distributed in the hope that it will be useful,
|
13 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
//GNU General Public License for more details.
|
16 |
//
|
17 |
//You should have received a copy of the GNU General Public License
|
18 |
//along with this program; if not, write to the Free Software
|
19 |
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
//--------------------------------------------------------------------------------
|
21 |
//Author contact information:
|
22 |
// David T. Ashley
|
23 |
// P.O. Box 918
|
24 |
// Marshall, Michigan, 49068
|
25 |
// dta@e3ft.com
|
26 |
//--------------------------------------------------------------------------------
|
27 |
//Pads a string on the left by zero until it reaches the
|
28 |
//target length. If the string is longer than the target length,
|
29 |
//it is truncated on the left.
|
30 |
//
|
31 |
//Unit tested 20060408.
|
32 |
//
|
33 |
function STRFUNC_pad_left_zero($str_in, $target_len)
|
34 |
{
|
35 |
if ($target_len >= 0) //Ignore invalid targets
|
36 |
{
|
37 |
$len = strlen($str_in);
|
38 |
|
39 |
if ($target_len == 0)
|
40 |
{
|
41 |
$str_in = "";
|
42 |
}
|
43 |
else if ($len < $target_len)
|
44 |
{
|
45 |
//The input string is too short. Pad it on the left with zeros.
|
46 |
$to_add = $target_len - $len;
|
47 |
for ($i = 0; $i < $to_add; $i++)
|
48 |
{
|
49 |
$str_in = "0". $str_in;
|
50 |
}
|
51 |
}
|
52 |
else if ($len > $target_len)
|
53 |
{
|
54 |
//The input string is too long. Remove the extra characters.
|
55 |
$to_remove = $len - $target_len;
|
56 |
|
57 |
$str_in = substr($str_in, $to_remove);
|
58 |
}
|
59 |
//In the final omitted else case, there is length equality, and hence nothing to do.
|
60 |
}
|
61 |
|
62 |
return($str_in);
|
63 |
}
|
64 |
//
|
65 |
//--------------------------------------------------------------------------------
|
66 |
//End of $RCSfile: strfunc_int.inc,v $.
|
67 |
//--------------------------------------------------------------------------------
|
68 |
?>
|