Jump to content

[SOLVED] insert character specified number of times


brendan6

Recommended Posts

im looking for a function that will create a string of a certain character a specified number of times.

 

ex.

 

$newstring = unknownfunction("*",5)
//resulting in $newstring being *****

 

NOTE: I know how to create a loop to do this, just wondering if such a function exists.

Im pretty sure there isnt one.. but you can do one pretty quick

check http://us2.php.net/strings to see if you got it.

 

function repeat($string,$number)

{

    $output= '';

    for($i=0;$i==$number; $i++)

    {

        $output .= $string;

    }

    return $output

}

Here is a working version of your function

 

<?php

function repeat($string,$number){
     $output= '';
     
     for($i=0;$i<$number; $i++){
        $output .= $string;
     }
     
    return $output;
}

echo repeat('*', 1);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.