limit a string to a certain number of characters

Links for php scripts

Moderators: macek, egami, gesf

bobaloo
New php-forum User
New php-forum User
Posts: 11
Joined: Sat Feb 01, 2003 11:34 am

limit a string to a certain number of characters

Postby bobaloo » Fri Feb 28, 2003 12:51 pm

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

Romantik
New php-forum User
New php-forum User
Posts: 67
Joined: Mon Feb 24, 2003 2:55 am
Location: ../Ukraine/Kherson
Contact:

Postby Romantik » Sat Mar 01, 2003 3:46 am

Code: Select all

<?
//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 :)

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Re: limit a string to a certain number of characters

Postby WiZARD » Sun Mar 02, 2003 12:13 am

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...


Return to “PHP Scripts”

Who is online

Users browsing this forum: allcapone1912 and 1 guest