[tips] limit paragraph characters php function

5:01 AM

(2) Comments

most of us need to limit paragraph characters , and then display read more button if you want to do such things you way use substr with strpos function but there is another thing you wanna do you dont want it to cut the string in the middle of the word , you want it to cut it after or before the word so you must use while loop and check if you are in space or word
to make it easier to you i modified a function to do such things

function limitChars($txt,$limit){
        if(strlen($txt) < $limit){
            return $txt; // return the text if less than limit
        }
              
        $post = substr($txt,$limit,1); // Find what is the last character displaying. We find it by getting only last one character from your display message.
       
        if($post !=" "){ // In this step, if last character is not " "(space) do this step .
       
            // Find until we found that last character is " "(space)
            // by $limit+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20)
            while($post !=" "){
                $i=1;
                $limit=$limit+$i;               
                $post = substr($txt,$limit,1);
            }
       
        }
       
        $post = substr($txt,0,$position); // Display your message
        return  $post."...";
    }

eslam

,

2 Responses to "[tips] limit paragraph characters php function"

eslam said :
October 28, 2010 at 10:55 AM
this is another function i found

function parseString($str,$minChars){
$charCount=0;
$output="";
$token = explode(" ", $str);
for( $i=0,$limit=count($token); $i<$limit; ++$i)
{
if($charCount < $minChars)
{
$output.=$token[$i] . " " ;
$charCount += strlen($token[$i]);
}
else
{
$output .="...";
break;
}
}
return $output;
}
March 29, 2011 at 11:57 AM
This is a nice article..
Its easy to understand ..
And this article is using to learn something about it..

c#, dot.net, php tutorial, Ms sql server

Thanks a lot..!
ri70

Post a Comment