Forum Moderators: coopster

Message Too Old, No Replies

How to echo a char "N" times

without looping

         

Gian04

1:28 pm on Jul 20, 2007 (gmt 0)

10+ Year Member



Is there a function wherein I can replace and display a character "N" without looping

Example:
$var1 = abc // output *** (3 asterisk)
$var2 = defghijklmn // *********** (11 asterisk)

coopster

2:41 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A regular expression would work or you could use str_repeat, using the length of the string as the multiplier argument.
<pre> 
<?php
$var1 = 'abc';
$var2 = 'defghijklmn';
print "$var1\n";
print "$var2\n";
print preg_replace('/./', '*', $var1) . "\n";
print preg_replace('/./', '*', $var2) . "\n";
print str_repeat('*', strlen($var1)) . "\n";
print str_repeat('*', strlen($var2)) . "\n";
?>
</pre>

Gian04

12:30 am on Jul 21, 2007 (gmt 0)

10+ Year Member



Thanks coopster,

str_repeat is the one I need