str_repeat

(PHP 4, PHP 5)

str_repeatВозвращает повторяющуюся строку

Описание

string str_repeat ( string $input , int $multiplier )

Возвращает строку input, повторенную multiplier раз.

Список параметров

input

Повторяемая строка.

multiplier

Количество раз, которые нужно повторить строку input.

multiplier должен быть больше или равен нулю. Если он равен нулю, возвращается пустая строка.

Возвращаемые значения

Возвращает повторяющуюся строку.

Примеры

Пример #1 Пример использования str_repeat()

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

Результат выполнения данного примера:

-=-=-=-=-=-=-=-=-=-=

Смотрите также

  • for
  • str_pad() - Дополняет строку другой строкой до заданной длины
  • substr_count() - Возвращает число вхождений подстроки

Коментарии

Автор:
str_repeat does not repeat symbol with code 0 on some (maybe all?) systems (tested on PHP Version 4.3.2 , FreeBSD 4.8-STABLE i386 ).

Use <pre>
while(strlen($str) < $desired) $str .= chr(0);
</pre> to have string filled with zero-symbols.
2003-07-21 13:45:52
http://php5.kiev.ua/manual/ru/function.str-repeat.html
Here is a shorter version of Kees van Dieren's function below, which is moreover compatible with the syntax of str_repeat:

<?php
function str_repeat_extended($input$multiplier$separator='')
{
    return 
$multiplier=='' str_repeat($input.$separator$multiplier-1).$input;
}
?>
2009-02-10 04:25:20
http://php5.kiev.ua/manual/ru/function.str-repeat.html
Автор:
Here is a simple one liner to repeat a string multiple times with a separator:

<?php
implode
($separatorarray_fill(0$multiplier$input));
?>

Example script:
<?php

// How I like to repeat a string using standard PHP functions
$input 'bar';
$multiplier 5;
$separator ',';
print 
implode($separatorarray_fill(0$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(0count($args), '?'));
print 
"\n";
?>

Example Output:
bar,bar,bar,bar,bar
?,?,?
2009-04-28 03:45:19
http://php5.kiev.ua/manual/ru/function.str-repeat.html
Автор:
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 :

<?php
$my_head 
str_repeat("°~"35);
echo 
strlen($my_head); // 105
echo mb_strlen($my_head'UTF-8'); // 70
?>

be carefull with characters and try to use mb_* package to make sure everything goes well ...
2011-10-23 07:51:03
http://php5.kiev.ua/manual/ru/function.str-repeat.html
function.str-repeat#90555

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

<?php

function spam($number) {
    return 
str_repeat('test'$number);
}

function 
spam2($number) {
    return 
implode(''array_fill(0$number'test'));
}

//echo spam(4);
$before microtime(true);
for (
$i 0$i 100000$i++) {
   
spam(10);
}
echo 
microtime(true) - $before "\n"// 0.010297
$before microtime(true);
for (
$i 0$i 100000$i++) {
   
spam2(10);
}
echo 
microtime(true) - $before// 0.032104
2018-06-29 14:22:07
http://php5.kiev.ua/manual/ru/function.str-repeat.html

    Поддержать сайт на родительском проекте КГБ