note 23283 added to ref.strings
| From: | dr dot helga at prickel-pit dot de | Date: | Sun, 14 Jul 2002 11:41:34 +0000 |
| Subject: | note 23283 added to ref.strings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-33046@lists.php.net to get a copy of this message | ||
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 ...
--
http://www.php.net/manual/en/ref.strings.php
http://master.php.net/manage/user-notes.php?action=edit+23283
http://master.php.net/manage/user-notes.php?action=delete+23283
http://master.php.net/manage/user-notes.php?action=reject+23283