Jump to content

Limiting No. Charcters


jfs0479

Recommended Posts

Hi Guys,

 

Just a quick query, please could someone instruct me as i am new to php coding how to limit the number of charcters when displaying something and having the ending as "..." For example displaying news articles and then a link saying Click Here For More

 

Thanks

 

James

Link to comment
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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