You are here

public function AbstractStringWrapper::strPad in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/zendframework/zend-stdlib/src/StringWrapper/AbstractStringWrapper.php \Zend\Stdlib\StringWrapper\AbstractStringWrapper::strPad()

Pad a string to a certain length with another string

Parameters

string $input:

int $padLength:

string $padString:

int $padType:

Return value

string

Overrides StringWrapperInterface::strPad

File

vendor/zendframework/zend-stdlib/src/StringWrapper/AbstractStringWrapper.php, line 228

Class

AbstractStringWrapper

Namespace

Zend\Stdlib\StringWrapper

Code

public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT) {
  if (StringUtils::isSingleByteEncoding($this
    ->getEncoding())) {
    return str_pad($input, $padLength, $padString, $padType);
  }
  $lengthOfPadding = $padLength - $this
    ->strlen($input);
  if ($lengthOfPadding <= 0) {
    return $input;
  }
  $padStringLength = $this
    ->strlen($padString);
  if ($padStringLength === 0) {
    return $input;
  }
  $repeatCount = floor($lengthOfPadding / $padStringLength);
  if ($padType === STR_PAD_BOTH) {
    $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
    $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
    $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
    $lastStringRightLength += $lastStringLength % 2;
    $lastStringLeft = $this
      ->substr($padString, 0, $lastStringLeftLength);
    $lastStringRight = $this
      ->substr($padString, 0, $lastStringRightLength);
    return str_repeat($padString, $repeatCountLeft) . $lastStringLeft . $input . str_repeat($padString, $repeatCountRight) . $lastStringRight;
  }
  $lastString = $this
    ->substr($padString, 0, $lengthOfPadding % $padStringLength);
  if ($padType === STR_PAD_LEFT) {
    return str_repeat($padString, $repeatCount) . $lastString . $input;
  }
  return $input . str_repeat($padString, $repeatCount) . $lastString;
}