Jump to content


Photo
- - - - -

Limiting string length


  • Please log in to reply
5 replies to this topic

#1 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 15 May 2006 - 03:46 PM

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 (:D) 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....

<?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
$var .= '...';
is?

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

e.g:
$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!

<?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 :D

Oh course if you dont want this as a function because you only want it as a one off, try this: ;)

// $data ->foo; = variable. (text).
echo (strlen( $data ->foo ) > $max) ? substr($data ->foo, 0, $max).'...' : $data ->foo;


#2 _*-cherries-_*

_*-cherries-_*
  • Guests

Posted 03 August 2006 - 03:56 AM

when you explain $var .= just tell them it means to append something onto a string, etc..

#3 Developer

Developer

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 03 August 2006 - 05:35 AM

This is the best way to do this, thnx for the tutorial :)
Another way (My WAY-Simply the same)
if(strlen($message) > 300)
{
	  $message = substr($message, 0, 300) . ' ...';
}

Edited by Developer, 03 August 2006 - 05:40 AM.


#4 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 03 August 2006 - 05:55 AM

Oh my lord this is old lol :)

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 :(

#5 _*-cherries-_*

_*-cherries-_*
  • Guests

Posted 04 September 2006 - 07:31 PM

Its just common english, you shouldn't really have to ask someone ;)

#6 Copernicus

Copernicus

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 04 September 2006 - 10:21 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users