Jump to content

Limiting No. Charcters


jfs0479

Recommended Posts

here ya go :-)

<?
function filter_charlimit($string, $length="50"){
if(strlen($string)<$length) return $string;
else return trim(substr($string, 0, $length)).'...';
}
echo filter_charlimit("i'm way to long to show the whole string in a wee tiny little textbox",20);
?>

 

basically... if $string is longer then $length, it cuts off the first $length characers and adds '...' if not, it returns the string untouched :-)

Link to comment
https://forums.phpfreaks.com/topic/47885-limiting-no-charcters/#findComment-234005
Share on other sites

Use the following function

 

<?php
function limitString($string, $maxlength)
{
if (strlen($string) > $maxlength)
{
	print nl2br(substr($string, 0, $maxlength)."...");
}
else
{
	print $string;
}
}
?>

 

and then on the text you want to limit, use

 

<?php limitString($text, 150); ?>

 

$text being your variable and the 150 marks the limit for the characters

 

Hope this helps

 

Ed

Link to comment
https://forums.phpfreaks.com/topic/47885-limiting-no-charcters/#findComment-234018
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.