Use at own risk. Programs haven't been thoroughly tested.
function: str_pad
Pad a string to a certain length with another string. Javascript clone of the PHP function str_pad. It acts exactly the same.
Info
@author Kevin van Zonneveld
@version 1.0
@link http://kevin.vanzonneveld.net
@param (string) pad_input The input string.
@param (string) pad_length If the value of pad_length is negative or less than the length of the input string, no padding takes place.
@param (string) pad_string Note: The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.
@param (string) pad_type Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
@version 1.0
@link http://kevin.vanzonneveld.net
@param (string) pad_input The input string.
@param (string) pad_length If the value of pad_length is negative or less than the length of the input string, no padding takes place.
@param (string) pad_string Note: The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.
@param (string) pad_type Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
Example
Usage
Outputs
"-=-=-Alien"
input = "Alien"; str_pad(input, 10, "-=", STR_PAD_LEFT);
Outputs
"-=-=-Alien"
Source Code
download sourcefunction str_pad (pad_input, pad_length, pad_string, pad_type) { if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; } if (pad_input.pad_length < pad_length) { pad_length = pad_length - pad_input.pad_length; if (pad_type == 'STR_PAD_LEFT') { pad_input = String(pad_string).str_repeat(pad_length) + pad_input; } else if (pad_type == 'STR_PAD_RIGHT') { pad_input = pad_input + String(pad_string).str_repeat(pad_length); } else if (pad_type == 'STR_PAD_BOTH') { pad_input = String(pad_string).str_repeat(pad_length / 2) + pad_input + String(pad_string).str_repeat(pad_length / 2); } } return pad_input; }
No comments. Be the first!