note 1385 deleted from String functions by torben
| From: | torben@php.net | Date: | Sun, 10 Sep 2000 22:53:28 +0000 |
| Subject: | note 1385 deleted from String functions by torben | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-4680@lists.php.net to get a copy of this message | ||
two functions missing from this list:
// Function: rpad
// ***********************************
// Purpose: right pad a string to a specified length,
// or trim it to that length if too long
// Inputs: $inStr - String to be padded
// $len - length of desired out put string
// $with - [optional] character(s) to use in padding
// (defaults to space " ")
// To Do: improper input error handling
// ***********************************
function rpad($inStr, $len, $with = " "){
// if inStr needs padding
if (strlen($inStr) < $len) {
$outStr = $inStr; // output string
// make sure $inStr + $with isn't longer than $len
if ((strlen($inStr) + strlen($with)) < $len) {
// add $with as many times as it will fit
for ($i=strlen($inStr);$i<$len;$i+=strlen($with)) {
$outStr .= $with;
}
}
// if its still not long enough
if (strlen($outStr) != $len) {
// tack on a enough of $with to make up the difference
$outStr .= substr($with,0,($len-strlen($outStr)));
}
// off to the races
return $outStr;
}
else {
// shrink it down
return substr($inStr,0,$len);
}
}
// Function: lpad
// ***********************************
// Purpose: left pad a string to a specified length,
// or trim it from the right to that length
// if too long
// Inputs: $inStr - String to be padded
// $len - length of desired out put string
// $with - [optional] character(s) to use in padding
// (defaults to space " ")
// To Do: improper input error handling
// ***********************************
function lpad($inStr, $len, $with = " ") {
// if inStr needs padding
if (strlen($inStr) < $len) {
$rStr = $inStr; // the string to be padded
$lStr = ""; // the padding
// make sure $inStr + $with isn't longer than $len
if ((strlen($inStr) + strlen($with)) < $len) {
// add $with to the padding as many times as it will fit
for ($i=strlen($inStr);$i<$len;$i+=strlen($with)) {
$lStr .= $with;
}
}
// stick the values together
$outStr = $lStr . $rStr;
// if its still not long enough
if (strlen($outStr) != $len) {
// tack on a enough of $with to the middle to make up the difference
$outStr = $lStr . substr($with,0,($len-strlen($outStr))) . $rStr;<br>
}
// off to the races
return $outStr;
}
// $inStr too long
else {
// shrink it down (chopping off the left most chars.)
return substr($inStr,-$len);
}
}