Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 42
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Na początku, kiedy zobaczyłem, że ktoś chce jakiejś opłaty za pomoc w tworzeniu stron ryknąłem śmiechem - potem przyszły problemy... i zaryzykowałem. Druga rzecz to: nie chciałem "kopiować". Ale prawda jest taka: są lepsi, bardziej doświadczeni i... czasem trzeba poprosić o pomoc, a jak poświęca się na to trzecią cześć życia, to nic dziwnego, że nie chce się swoich "sekretów" zdradzać za darmo. Skorzystałem z "algorytmy.pl" i naprawdę jestem z tego w 100% zadowolony, polecam - dla zawodowców (co się uczą) i amatorów (można skorzystać z gotowego rozwiązania).

Tomasz Czypicki
Cybernoxa

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

str_repeat

(PHP 4, PHP 5)

str_repeat -- Repeat a string

Description

string str_repeat ( string input, int multiplier )

Returns input_str repeated multiplier times. multiplier has to be greater than or equal to 0. If the multiplier is set to 0, the function will return an empty string.

Przykład 1. str_repeat() example

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

Powyższy przykład wyświetli:

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

See also for, str_pad(), and substr_count().




User Contributed Notes

15-Sep-2005 04: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
?>


Roland Knall<roland dot knall at aerie dot at>
22-Aug-2005 10:41

You can use this function for a simple leading zero function. I use it the following way:

   function lzero ( $nr, $length = 2 ) {
       if ( strlen ( trim ( $nr ) ) >= $length
           return $nr;
       return str_repeat ( "0", $length strlen ( trim ( $nr ) ) ).$nr;
   }

The trim is just for extra safety, so that I do not count whitespaces


pawan at shopsbs dot com
05-Jul-2005 09:22

Thanks for the code Gal Chen

I was having trouble using the str_pad function to pad my text (I could never find a reason why). Your code just worked just fine. I just fine tuned it to add a piece of sting (default is a <space>)

Code:

function str_repeattolength ($str, $length, $end = "&nbsp;") {
  $strlength= strlen($str);
  if ($strlength>= $length) {
   return substr($str,0,$length) . $end;
  } else {
   return $str . str_repeattolength($str, $length - $strlength) . $end;
  }
}


21-Jul-2003 07: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.


abodeman at enoughspamalready dot yahoo dot com
27-May-2003 11:35

Recursive functions are almost always slower than the corresponding iterative function. Therefore, dmarsh's function will be faster than Gail's.


Gal Chen
18-Mar-2003 04:18

a response to dmarsh
if u do wanna write a function that fills a string to a length its best to use a recoursive function

function str_repeattolength ($str, $length) {
  $strlength= strlen($str);
  if ($strlength>= $length) {
   return substr($str,0,$length);
  } else {
   return $str . str_repeattolength($str, $length - $strlength);
  }
}

or just use str_pad


bob at bobarmadillo dot com
21-Nov-2002 01:26

While dmarsh's function is nice it duplicates the function str_pad().
str_repeat is good when you want the entire string repeated.
For instance, if you want to put 6 &nbsp;'s in somewhere, str_pad will cut it off at odd places whereas str_repeat will return 6 full &nbsp's.

the following will return the same result as dmarsh's code.

$t = "-=-";
print str_pad('',0,$t)."\n";
print str_pad('',1,$t)."\n";
print str_pad('',2,$t)."\n";
...etc.


dmarsh dot NO dot SPAM dot PLEASE at spscc dot ctc dot edu
18-Sep-2002 01:15

If you need an alternate str_repeat function that outputs an exact length using a particular input string to fill that length (as opposed to repeating the input string), try this little gem:

<?php

// use this function:
function str_repeat2($input, $length) {
// returns the output exactly $length using $input to fill that length
$answer="";

if (
$length>=1 && strlen($input)>=1) {
$answer = substr(str_repeat($inputceil($length/strlen($input)) ), 0, $length);
}

return
$answer;
}

//demo the function's use:
$t="-=-";
print
str_repeat2($t,0)."\n";
print
str_repeat2($t,1)."\n";
print
str_repeat2($t,2)."\n";
print
str_repeat2($t,3)."\n";
print
str_repeat2($t,4)."\n";
print
str_repeat2($t,5)."\n";
print
str_repeat2($t,6)."\n";
?>


dakota at dir dot bg
25-Jun-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-Oct-2001 01: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.


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt