str_repeat
(PHP 4, PHP 5)
str_repeat — Repeat a string
Description
string str_repeat
( string
$input
, int $multiplier
)
Returns input
repeated
multiplier
times.
Parameters
-
input
-
The string to be repeated.
-
multiplier
-
Number of time the
input
string should be repeated.multiplier
has to be greater than or equal to 0. If themultiplier
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
- for
- str_pad() - Pad a string to a certain length with another string
- substr_count() - Count the number of substring occurrences
- addcslashes
- addslashes
- bin2hex
- chop
- chr
- chunk_split
- convert_cyr_string
- convert_uudecode
- convert_uuencode
- count_chars
- crc32
- crypt
- echo
- explode
- fprintf
- get_html_translation_table
- hebrev
- hebrevc
- hex2bin
- html_entity_decode
- htmlentities
- htmlspecialchars_decode
- htmlspecialchars
- implode
- join
- lcfirst
- levenshtein
- localeconv
- ltrim
- md5_file
- md5
- metaphone
- money_format
- nl_langinfo
- nl2br
- number_format
- ord
- parse_str
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1_file
- sha1
- similar_text
- soundex
- sprintf
- sscanf
- str_getcsv
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr_compare
- substr_count
- substr_replace
- substr
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap
Коментарии
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==0 ? '' : str_repeat($input.$separator, $multiplier-1).$input;
}
?>
Here is a simple one liner to repeat a string multiple times with a separator:
<?php
implode($separator, array_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($separator, array_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(0, count($args), '?'));
print "\n";
?>
Example Output:
bar,bar,bar,bar,bar
?,?,?
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 ...
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