note 23283 deleted from ref.strings by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 15 Sep 2004 20:48:50 +0000 |
| Subject: | note 23283 deleted from ref.strings by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76794@lists.php.net to get a copy of this message | ||
Note Submitter: dr dot helga at prickel-pit dot de
----
For generating a Field-Number File I needed a function, wich can add chars in front or at the end of
a string to match a certain string length. I couldn't find it in php so here's the
function I wrote to solve the problem:
--------------CUT------------------
function addChars($string,$char,$endlength,$dir) {
$i=0;
$stringlength=strlen($string);
$addchar_lenght=$endlength-$stringlength;
$addchar_string="";
while ($i<$addchar_lenght) {
$addchar_string.=$char;
$i++;
}
if ($dir==1) {
$new_string=$string.$addchar_string;
}
if ($dir==0) {
$new_string=$addchar_string.$string;
}
return $new_string;
}
----------------/CUT-----------------
$string is the original string
$char is the character which shall be added
$endlength is the length of the new string
$dir specifies if the chars shall be added in front (0) or at the end (1) of the string
example:
"1234" should be "00001234":
$text1="1234";
$text2=addChars($text,"0",8,0)
hope someone will find it usefull ...