Help - Search - Members - Calendar
Full Version: Limiting string length
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Matthew.
Intro..
This tutorial is a very simple one on cutting a string short,
During it, if you bother to read it and not just copy the code (bigwink.gif) you will learn how to determine the length of a string and how to take portions of strings.

I apoagise for not submitting any complicated tutorials but i am really too tired. So this is my lame attempt.

The functions used are....

strlen();

substr();



The final code:

Ok, lets get it out of the way....

CODE
<?php

$text = "Hi my name is Matt";
    
    function cutit ($var, $max)
    {
            
         $length = strlen( $var );
          
          if($length > $max)
          {
                
                $var = substr($var, 0, $max);
                $var .= '...';
          }
          
          return $var;
    }
    
    
echo cutit($text, '4');
?>



The write up

Heres what we are doing here in pseudocode within the function.

get string length
IF string length is greater than max length
> use substr to cut string length to max length
> add ... to the end of string

return the final value of new text.


The functions....

strlen is used to return the length of a string. strlen ( string )

substr
returns a part of a string, where the start charactor and end charactor are set. substr ( string, (int) start, ([-]int) end )


You may just be wondering, as a final note what
CODE
$var .= '...';
is?

Well, .= means $var = previous value of($var) + 'new string';

e.g:
CODE
$string = "hi";
$string .= ", hello";

echo $string; // Would return 'hi, hello'.



Final code....again

Here is some smaller code that does the same thing but with less lines for those who wish to save space lol!

CODE
<?php
$text = "Hi my name is Matt";
    
    function cutit ($var, $max)
    {
        return (strlen( $var ) > $max) ? substr($var, 0, $max).'...' : $var;
        
    }
    
// Testing +Usage.    
echo cutit($text, '4');
?>


Does the same thing bigwink.gif

Oh course if you dont want this as a function because you only want it as a one off, try this: bigwink.gif

CODE
// $data ->foo; = variable. (text).
echo (strlen( $data ->foo ) > $max) ? substr($data ->foo, 0, $max).'...' : $data ->foo;
-cherries-
when you explain $var .= just tell them it means to append something onto a string, etc..
Developer
This is the best way to do this, thnx for the tutorial smile.gif
Another way (My WAY-Simply the same)
CODE
if(strlen($message) > 300)
{
      $message = substr($message, 0, 300) . ' ...';
}
Matthew.
Oh my lord this is old lol victory.gif

And Developer what you do is exactly the same as what i am doing, its just that in the tutorial i used a tenary operator instead of an if. (on the final final code which is different)

And Cherries, ask someone starting out what append means lol victory.gif
-cherries-
Its just common english, you shouldn't really have to ask someone tongue.gif
Copernicus
Nice tutorial, just figured out how to fix string lengths the other day!

Like how vBulletin shows tooltips of the thread text....

"This is the thread text and we all kn..."

Limits it to x characters and adds "..."

Very handy!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.