Use PHP functions in JavaScript

JavaScript str_repeat

Returns the input string repeat mult times

1
2
3
4
56
7
8
9
1011
12
13
function str_repeat ( input, multiplier ) {
    // Returns the input string repeat mult times  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/str_repeat    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: str_repeat('-=', 10);
    // *     returns 1: '-=-=-=-=-=-=-=-=-=-='
        
    return new Array(multiplier+1).join(input); 
}
external links: original PHP docs | raw js source

Examples

Running

1
str_repeat('-=', 10);

Should return

1
'-=-=-=-=-=-=-=-=-=-='

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have str_repeat goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
snaky
27 Jul '08 Permalink

q  thats grat :-) thank you for publishing

Gravatar
Kevin van Zonneveld
13 Apr '08 Permalink

q  @ Jonas Raoni: It sure is, thanks a lot!

Gravatar
Jonas Raoni
12 Apr '08 Permalink

q  This way it's faster :)

String.prototype.repeat = function(l){
return new Array(l+1).join(this);
};

alert("jonas".repeat(10));

Gravatar
Kevin van Zonneveld
9 Jan '08 Permalink

q  @ Darell Brogdon: Thanks for pointing that out, I've searched hard, but wasn't able to find similar projects. Now that I did, how would you feel if I included some of your functions to this project? Well creditted of course.
Most functions are already here but I found a couple that would be a great addition.

The reason to continue here is I developed some tools that really help me to publish new functions quickly, and for me it would be a buzz killer to have to maintain this on sourceforge. Exposure here is bigger as well I think.

I do believe however that once php.js reaches version 1.0, housing it on such a platform would be a better idea.

Gravatar
Darrell Brogdon
9 Jan '08 Permalink

q  Here's a similar project I started awhile back if you're interested: http://sourceforge.net/projects/php4js I haven't had much time to devote to it but I would love it if someone could breathe new life into it.


Contribute a New function