SunshinePHP Developer Conference 2015

str_repeat

(PHP 4, PHP 5)

str_repeat문자열을 반복

설명

string str_repeat ( string $input , int $multiplier )

multiplier번 반복한 input을 반환합니다.

인수

input

반복할 문자열.

multiplier

input 문자열을 반복할 횟수.

multiplier는 0 이상이여야 합니다. multiplier를 0으로 설정하면, 빈 문자열을 반환합니다.

반환값

반복한 문자열을 반환합니다.

예제

Example #1 str_repeat() 예제

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

위 예제의 출력:

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

참고

  • for
  • str_pad() - 문자열을 지정한 길이가 되도록 다른 문자열로 채웁니다
  • substr_count() - Count the number of substring occurrences

add a note add a note

User Contributed Notes 7 notes

up
2
claude dot pache at gmail dot com
5 years ago
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;
}
?>
up
3
Anonymous
3 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 :

<?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 ...
up
1
Alper Kaya
7 years ago
If you want to hide a part of your password, you can use this code. It's very simple and might be required in your user management panel.

<?php
$password
= "12345abcdef";
$visibleLength = 4; // 4 chars from the beginning

echo substr($password,0,4).str_repeat("*", (strlen($password)-$visibleLength));
?>
up
0
Anonymous
11 years ago
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.
up
-1
Damien Bezborodov
5 years ago
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
?,?,?
up
-2
bryantSPAMw at geocities dot SPAM dot com
12 years ago
(For the benefit of those searching the website:)

This is the equivalent of Perl's "x" (repetition) operator, for eg.  str_repeat("blah", 8) in PHP does the same thing as "blah" x 8 in Perl.
up
-9
Anonymous
9 years ago
In reply to what Roland Knall wrote:

It is much simpler to use printf() or sprintf() for leading zeros.

<?php
   printf
("%05d<br>\n"1); // Will echo 00001
  
sprintf("%05d<br>\n"1); // Will return 00001
?>
To Top