downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

str_replace> <str_pad
[edit] Last updated: Wed, 22 May 2013

view this page in

str_repeat

(PHP 4, PHP 5)

str_repeatRepite un string

Descripción

string str_repeat ( string $input , int $multiplier )

Devuelve el input repetido multiplier veces.

Parámetros

input

El string a ser repetido.

multiplier

El número de veces que el string input debe ser repetido.

multiplier debe ser mayor o igual a 0. Si el multiplier se establece en 0, la función devolverá un string vacío.

Valores devueltos

Devuelve el string repetido.

Ejemplos

Ejemplo #1 Ejemplo de str_repeat()

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

El resultado del ejemplo sería:

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

Ver también

  • for
  • str_pad() - Rellena un string hasta una longitud determinada con otro string
  • substr_count() - Cuenta el número de apariciones del substring



str_replace> <str_pad
[edit] Last updated: Wed, 22 May 2013
 
add a note add a note User Contributed Notes str_repeat - [11 notes]
up
3
Anonymous
1 year 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
5 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
1
Anonymous
7 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
?>
up
0
Damien Bezborodov
4 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
0
Anonymous
9 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
0
bryantSPAMw at geocities dot SPAM dot com
11 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
-1
dakota at dir dot bg
10 years ago
Note that the first argument is parsed only once, so it's impossible to do things like this:

echo str_repeat(++$i, 10);

The example will produce 10 times the value of $i+1, and will not do a cycle from $i to $i+10.
up
-1
r3d dot w0rm at yahoo dot com
3 years ago
str_repeat() Function Integer Overflow

For more info see :

http://bugs.php.net/bug.php?id=51105
up
-1
claude dot pache at gmail dot com
4 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
Kees van Dieren
4 years ago
Needed a function to repeat a string with a separator.

<?php
/**
 * Repeats <tt>$string</tt> <tt>$multiplier</tt> times, separated with <tt>$sep</tt>.
 *
 * str_repeat_sep('?', ',', 3) ==> "?,?,?"
 * str_repeat_seap('..', '/', 3) ==> "../../.."
 *
 * @param string $string
 * @param string $sep
 * @param int $multiplier
 * @return string
 */
function str_repeat_sep($string, $sep, $multiplier) {
 
$ret = "";
  for(
$i=0;$i<$multiplier;$i++) {
    if (
$i) $ret.=$sep;
   
$ret.=$string;
  }
  return
$ret;
}
?>
up
-2
divinity76 at gmail dot com
1 year ago
no idea how many times i have written this.
function br($times=1,$definition="<br/>\n")
{
echo str_repeat($definition,$times);
}

 
show source | credits | sitemap | contact | advertising | mirror sites