phpjs
Version:
102 lines (84 loc) • 3.18 kB
Markdown
layout: page
title: "JavaScript str_pad function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/str_pad:525
- /functions/view/str_pad
- /functions/view/525
- /functions/str_pad:525
- /functions/525
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's str_pad
{% codeblock strings/str_pad.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/str_pad.js raw on github %}
function str_pad (input, pad_length, pad_string, pad_type) {
// From: http://phpjs.org/functions
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + namespaced by: Michael White (http://getsprink.com)
// + input by: Marco van Oort
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
// * returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
// * example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
// * returns 2: '------Kevin van Zonneveld-----'
var half = '',
pad_to_go;
var str_pad_repeater = function (s, len) {
var collect = '',
i;
while (collect.length < len) {
collect += s;
}
collect = collect.substr(0, len);
return collect;
};
input += '';
pad_string = pad_string !== undefined ? pad_string : ' ';
if (pad_type !== 'STR_PAD_LEFT' && pad_type !== 'STR_PAD_RIGHT' && pad_type !== 'STR_PAD_BOTH') {
pad_type = 'STR_PAD_RIGHT';
}
if ((pad_to_go = pad_length - input.length) > 0) {
if (pad_type === 'STR_PAD_LEFT') {
input = str_pad_repeater(pad_string, pad_to_go) + input;
} else if (pad_type === 'STR_PAD_RIGHT') {
input = input + str_pad_repeater(pad_string, pad_to_go);
} else if (pad_type === 'STR_PAD_BOTH') {
half = str_pad_repeater(pad_string, Math.ceil(pad_to_go / 2));
input = half + input + half;
input = input.substr(0, pad_length);
}
}
return input;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/str_pad.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/str_pad.js)
### Example 1
This code
{% codeblock lang:js example %}
str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'-=-=-=-=-=-Kevin van Zonneveld'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'------Kevin van Zonneveld-----'
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}