PHP Doku:: Wiederholt einen String - function.str-repeat.html

Verlauf / Chronik / History: (14) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzTextverarbeitungZeichenkettenString-Funktionenstr_repeat

Ein Service von Reinhard Neidl - Webprogrammierung.

String-Funktionen

<<str_pad

str_replace>>

str_repeat

(PHP 4, PHP 5)

str_repeatWiederholt einen String

Beschreibung

string str_repeat ( string $input , int $multiplier )

Gibt input multiplier mal zurück.

Parameter-Liste

input

Die zu wiederholende Zeichenkette.

multiplier

Die Anzahl der Wiederholungen, die auf input angewendet werden sollen.

multiplier muss größer als oder gleich 0 sein. Wenn multiplier den Wert 0 hat, gibt die Funktion einen leeren String zurück.

Rückgabewerte

Gibt die wiederholte Zeichenkette zurück.

Beispiele

Beispiel #1 str_repeat()-Beispiel

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch

  • for
  • str_pad() - Erweitert einen String unter Verwendung eines anderen Strings auf eine bestimmte Länge
  • substr_count() - Ermittelt, wie oft eine Zeichenkette in einem String vorkommt


9 BenutzerBeiträge:
- Beiträge aktualisieren...
r3d dot w0rm at yahoo dot com
22.02.2010 9:52
str_repeat() Function Integer Overflow

For more info see :

http://bugs.php.net/bug.php?id=51105
Damien Bezborodov
28.04.2009 9:45
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
?,?,?
claude dot pache at gmail dot com
10.02.2009 11:25
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;
}
?>
Kees van Dieren
16.01.2009 10:26
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;
}
?>
Alper Kaya
30.06.2007 12:09
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));
?>

15.09.2005 16:32
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
?>

21.07.2003 19:45
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.
dakota at dir dot bg
25.06.2002 12:06
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.
bryantSPAMw at geocities dot SPAM dot com
25.10.2001 13:16
(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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)