PHP: Hypertext Preprocessor (original) (raw)

str_repeat

(PHP 4, PHP 5, PHP 7, PHP 8)

str_repeat — Repeat a string

Description

Parameters

string

The string to be repeated.

times

Number of time the string string should be repeated.

times has to be greater than or equal to 0. If the times is set to 0, the function will return an empty string.

Return Values

Returns the repeated string.

Examples

Example #1 str_repeat() example

<?php echo str_repeat("-=", 10); ?>

The above example will output:

See Also

Found A Problem?

Damien Bezborodov

16 years ago

`Here is a simple one liner to repeat a string multiple times with a separator:

multiplier,multiplier, multiplier,input)); ?>

Example script:
multiplier,multiplier, multiplier,input)); print "\n";// Say, this comes in handy with count() on an array that we want to use in an // SQL query such as 'WHERE foo IN (...)' $args = array('1', '2', '3'); print implode(',', array_fill(0, count($args), '?')); print "\n"; ?>

Example Output:
bar,bar,bar,bar,bar
?,?,?

`

Alexander Ovsiyenko

6 years ago

`http://php.net/manual/en/function.str-repeat.php#90555

Damien Bezborodov , yeah but execution time of your solution is 3-5 times worse than str_replace.

i<100000;i < 100000; i<100000;i++) { spam(10); } echo microtime(true) - $before , "\n"; // 0.010297 $before = microtime(true); for ($i = 0; i<100000;i < 100000; i<100000;i++) { spam2(10); } echo microtime(true) - $before; // 0.032104 ` [ **_claude dot pache at gmail dot com_**](#88830)[ ¶](#88830) **16 years ago** `Here is a shorter version of Kees van Dieren's function below, which is moreover compatible with the syntax of str_repeat: multiplier,multiplier, multiplier,separator='') { return multiplier==0?′′:strrepeat(multiplier==0 ? '' : str_repeat(multiplier==0?′′:strrepeat(input.$separator, multiplier−1).multiplier-1).multiplier1).input; } ?>

`

Anonymous

13 years ago

`hi guys ,
i've faced this example :
<?php

$my_head

= str_repeat("°~", 35);
echo $my_head;?>

so , the length should be 35x2 = 70 !!!
if we echo it :

be carefull with characters and try to use mb_* package to make sure everything goes well ...

`