note 25278 deleted from function.str-repeat by nlopess
| From: | nlopess@php.net | Date: | Sun, 20 May 2007 13:45:35 +0000 |
| Subject: | note 25278 deleted from function.str-repeat by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126736@lists.php.net to get a copy of this message | ||
Note Submitter: dmarsh dot NO dot SPAM dot PLEASE at spscc dot ctc dot edu
----
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($input, ceil($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";
?>