limit a string to a certain number of characters

A

Anonymous

Guest
this is a function i wrote to do this, because apparently php doesn't have one built-in.

<?
function limit_string ($string, $limit){
echo substr($string, 0, (strlen($string)*($limit/strlen($string))));}

//this:
limit_string("12345",3);
//displays "123"

// this:
limit_string("12345",6);
//displays "12345"

//and this:
limit_string("12345",0);
//dislpays nothing
?>


it works great! :D
 
Code:
<?
//this:
echo substr("12345",0,3);
//displays "123"

// this:
echo substr("12345",0,6);
//displays "12345"

//and this:
echo substr("12345",0,0);
//dislpays nothing
?>

It works great too :)
 
Hi!
Romantik is right if you carefully read the PHP manual you see what mach more function is inside of PHP. In a future before some invent, try to find solution in maual of PHP...
 
Back
Top