limit a string to a certain number of characters

Links for php scripts

Moderators: egami, macek, gesf

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

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:

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 :)
PHP+MySql= Love :)

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

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...
"Sex,Drugs and Rock&Roll " replaced at "Sucks,Bugs and Plug&Play";
Image

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest