I am trying to limit the amount of text/data is being shown from MySQL not MySQL LIMIT but limiting it on the actual page like most blogs do. After certain text point they just display ...more, I know there is a function to do this in PHP but I am unable to remember its name could some one help me out with it?
if(strlen($text)>1000){
$text=substr($text,0,1000).' Read more';
}
you should understand that it can cut words and tags too.
There are a few ways to do it, the easiest probably being substr()
$short = substr($long, 0, $max_len);
string substr ( string $string , int $start [, int $length ] )
It accepts two arguments. The first is the string that you would like to trim. The second is the length, in characters of what you'd like returned.
You could use a function like this (taken from http://brenelz.com/blog/creating-an-ellipsis-in-php/):
function ellipsis($text, $max=100, $append='…')
{
if (strlen($text) <= $max) return $text;
$out = substr($text,0,$max);
if (strpos($text,' ') === FALSE) return $out.$append;
return preg_replace('/\w+$/','',$out).$append;
}
This won't cut a word in half like substr.
<?
$position=14; // Define how many character you want to display.
$message="You are now joining over 2000 current";
$post = substr($message, 0, $position);
echo $post;
echo "...";
?>
This result shows 14 characters from your message
SELECT LEFT(content, 1000) FROM blog
If you load entire content for example 30 000 chars and do substr()
, you are wasting memory in order to show only 1000.
Apply the wrap() function to get your shortened text, and replace "99" with the number of characters you want to limit it to.
function wrap($string) {
$wstring = explode("
", wordwrap($string, 99, "
") );
return $wstring[0];
}