Pad a UTF-8 string to a certain length with another string.

@param  string $s UTF-8 encoding
@param  int    $length
@param  string $pad

@return string
18618
public static function padLeft(string $s, int $length, string $pad = ' '): string
    {
        $length -= \mb_strlen($s);
        if ($length <= 0) {
            return $s;
        }

        $padLen = \mb_strlen($pad);

        return \str_repeat($pad, (int) ($length / $padLen)) . \mb_substr($pad, 0, $length % $padLen) . $s;
    }
986414
public static function mb_str_pad(string $input, int $pad_length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT, string $encoding = "UTF-8")
    {
        $input_length = mb_strlen($input);
        switch ($pad_type) {
            case STR_PAD_RIGHT :
                return $input . self::mb_fill_string($pad_string, $pad_length - $input_length);
            case STR_PAD_LEFT :
                return self::mb_fill_string($pad_string, $pad_length - $input_length) . $input;
            case STR_PAD_BOTH :
                return self::mb_fill_string($pad_string, (int) floor(($pad_length - $input_length) / 2), $encoding)
                    . $input
                    . self::mb_fill_string($pad_string, (int) ceil(($pad_length - $input_length) / 2), $encoding);
            default :
                throw new \InvalidArgumentException("mb_str_pad(): Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT or STR_PAD_BOTH");
        }
    }
948276
protected function pad($string, $len)
    {
        $strlen = $this->strlen($string);
        if ($strlen > $len) return $string;

        $pad = $len - $strlen;
        return $string . str_pad('', $pad, ' ');
    }
1129777
public function stringPad($input, $pad_length, $pad_string = ' ')
    {
        $count = strlen($input);

        // Fix μ issues
        if(strpos($input, 'μ'))
            $count--;

        $space = $pad_length - $count;

        return str_repeat($pad_string, $space) . $input;
    }
868116
protected function setto($s,$length)
    {
        $this->b = substr_replace($this->b,$s,$this->j+1);
        $this->k = $this->j+$length;
    }
1079621
private function mbStrPad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = NULL)
    {
        $encoding = $encoding === NULL ? mb_internal_encoding() : $encoding;
        $padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
        $padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
        $pad_len -= mb_strlen($str, $encoding);
        $targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
        $strToRepeatLen = mb_strlen($pad_str, $encoding);
        $repeatTimes = ceil($targetLen / $strToRepeatLen);
        $repeatedString = str_repeat($pad_str, max(0, $repeatTimes)); // safe if used with valid utf-8 strings
        $before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : '';
        $after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : '';
        return $before . $str . $after;
    }
843101
public function pad($length, $padding = ' ') {
		$len = $length - $this->length();
		return $this->applyPadding(floor($len / 2), ceil($len / 2), $padding);
	}
721366
public static String pad( String s, int length )
	{
		// Trim if longer...
		if( s.length() > length )
		{
			return s.substring( 0, length );
		}
		StringBuffer buffer = new StringBuffer(s);
		int spaces = length - s.length();
		while( spaces-- > 0 )
		{
			buffer.append(' ');
		}
		return buffer.toString();
	}
376965
public static function padRight(string $string, int $length, string $filler = ' '): string
    {
        while (self::length($string) <= ($length - self::length($filler))) {
            $string = $string . $filler;
        }
        return $string;
    }
538588
public function pad($pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT)
        {
            $string = $this->get();
            $value = str_pad($string, $pad_length, $pad_string, $pad_type);
            $this->update($value);
            return $this;
        }
805704
public static function encodeString(string $s, int $maxLength = null): string
    {
        static $table;
        if ($table === null) {
            foreach (\array_merge(\range("\x00", "\x1F"), \range("\x7F", "\xFF")) as $ch) {
                $table[$ch] = '\x' . \str_pad(\dechex(\ord($ch)), 2, '0', STR_PAD_LEFT);
            }
            $table['\\'] = '\\\\';
            $table["\r"] = '\r';
            $table["\n"] = '\n';
            $table["\t"] = '\t';
        }

        if ($maxLength && \strlen($s) > $maxLength) { // shortens to $maxLength in UTF-8 or longer
            $s = \mb_substr($tmp = $s, 0, $maxLength);
            $shortened = $s !== $tmp;
        }

        if (\preg_match('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', $s) || \preg_last_error()) { // is binary?
            if ($maxLength && \strlen($s) > $maxLength) {
                $s = \substr($s, 0, $maxLength);
                $shortened = true;
            }
            $s = \strtr($s, $table);
        }

        return $s . (empty($shortened) ? '' : ' ... ');
    }
416929
public static function pad($input, $length = 0)
    {
        $input = ltrim($input, '0');

        if ($input == '') {
            $input = '0';
        }

        return str_pad($input, $length, '0', STR_PAD_LEFT);
    }
1368108
def _pad(self, b):
        
        return b + (self.bs - len(b) % self.bs) * chr(self.bs - len(b) % self.bs).encode("UTF-8")
820415
public static function asciiRand($length, $subset = self::ASCII_ALL) {
            switch ($subset) {
                case self::ASCII_ALPHANUM:
                    $subset = self::$asciiAlphanum;
                    break;
                case self::ASCII_NONALPHANUM:
                    $subset = self::$asciNonAlphanum;
                    break;
                default:
                    $subset = array_merge(self::$asciiAlphanum, self::$asciNonAlphanum);
            }

            $count = count($subset) - 1;

            $r = '';

            for ($i = 0; $i < $length; $i++) {
                $r .= $subset[mt_rand(0, $count)];
            }

            return $r;
        }
300064
public static function str_pad(&$str, int $pad_length, $pad_string=' ', int $pad_type=\STR_PAD_RIGHT) {
		cast::string($str, true);
		cast::string($pad_string, true);

		$current_length = v_mb::strlen($str);
		$pad_string_length = v_mb::strlen($pad_string);

		if ($pad_length <= $current_length || ! $pad_string_length) {
			return;
		}

		// Pad left.
		if (\STR_PAD_LEFT === $pad_type) {
			$str = \str_repeat($pad_string, \ceil(($pad_length - $current_length) / $pad_string_length)) . $str;
			$new_length = v_mb::strlen($str);
			if ($new_length > $pad_length) {
				$str = v_mb::substr($str, $new_length - $pad_length, null);
			}
		}
		// Pad both.
		elseif (\STR_PAD_BOTH === $pad_type) {
			$leftright = 'right';
			while (v_mb::strlen($str) < $pad_length) {
				$leftright = 'left' === $leftright ? 'right' : 'left';
				if ('left' === $leftright) {
					$str = "{$pad_string}{$str}";
				}
				else {
					$str .= $pad_string;
				}
			}

			$new_length = v_mb::strlen($str);
			if ($new_length > $pad_length) {
				if ('left' === $leftright) {
					$str = v_mb::substr($str, $new_length - $pad_length, null);
				}
				else {
					$str = v_mb::substr($str, 0, $pad_length);
				}
			}
		}
		// Pad right.
		else {
			$str .= \str_repeat($pad_string, \ceil(($pad_length - $current_length) / $pad_string_length));
			$new_length = v_mb::strlen($str);
			if ($new_length > $pad_length) {
				$str = v_mb::substr($str, 0, $pad_length);
			}
		}
	}
19429
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;
    }
262964
protected function pad($src, $pad, $len = 16)
    {
        return substr(hash('sha1', sprintf('%s%s', $src, $pad)), 0, $len);
    }