I’ve written a very simple jQuery function to return a string padded to a specified length, similar to the php equivalent str_pad.
$.strPad = function(i,l,s) {
var o = i.toString();
if (!s) { s = '0'; }
while (o.length < l) {
o = s + o;
}
return o;
};
Example Usage:
$.strPad(12, 5); // returns 00012
$.strPad('abc', 6, '#'); // returns ###abc
This version only supports left padding, which is why it is labelled as only a simple version
.
This is great, there are a lot of PHP-related scripting functions that would be handy if they were accessible in JavaScript – this takes us a step closer!
As you mention, the option to choose which side of the text to pad would also be great
@Wardy: Thanks
– There is actually an open source project (PHP.js) available at http://phpjs.org/ that is attempting to port suitable PHP functions to JavaScript.
Sweet. Thanks.
Pingback: [jQuery] String formatieren - Delphi-PRAXiS
Pingback: How to add leading zeroes in a php variable « Data Integrated Entity