Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : str_repeat

str_repeat

Repeat a string (PHP 4, PHP 5)
string str_repeat ( string input, int multiplier )

Example 2445. str_repeat() example

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

The above example will output:

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

Related Examples ( Source code ) » str_repeat


Code Examples / Notes » str_repeat

21-jul-2003 06: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

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.


15-sep-2005 03:32

In reply to what Roland Knall wrote:
It is much simpler to use printf() or sprintf() for leading zeros.
<?php
  printf("%05d
\n",  1); // Will echo 00001
  sprintf("%05d
\n",  1); // Will return 00001
?>


alper kaya

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));
?>


bryantspamw

(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.


Change Language


Follow Navioo On Twitter
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
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
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
eXTReMe Tracker