Jump to content

limit text?


Maverickb7

Recommended Posts

Hello, I'm trying to limit the description of articles on my front page and can't seem to figure out how to do something. If the string is longer then X then it should be cut off and '...' three dots like that should be put at the end. If the string is shorter nothing will be done and no dots will appears. I've got it to got the string if its longer but I have no clue how to make it not do anything and not add the three dots if the string is shorter. Can anyone help? my code is below.

[code]function limitWithTags ($text, $length) {
        $text = str_replace(array('<BR>', '<br>', '<BR/>', '<br/>', '<BR />', '<br />'), '', $text);
        $len = strlen($text);
        $count = 0;

        $intag = 0;
        $inendtag = 0;
        $limit = 0;

        while ($i < $len) {

               switch ($c = $text{$i}) {

               case '<':
                        $intag = 1;
                        $short .= $c;
                        if ($text[$i+1]=='/') $inendtag = 1;
                        break;
               case '>':
                        $intag = 0;
                        $short .= $c;
                        if ($text[$i-1]=='/') $inendtag = 1;
                        if ($limit && $inendtag) break 2;
                        $inendtag = 0;
                        break;
               default:
                       if (!$intag) {
                            if ($count++ < $length)
                                $short .= $c;
                            else
                                $limit = 1;
                       }
                       else
                           $short .= $c;
               }
               ++$i;
        }
        $short = trim($short, ' ,');  // remove spaces and commas from end
        return $short . '...';
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/8068-limit-text/
Share on other sites

[!--quoteo(post=367279:date=Apr 21 2006, 02:29 PM:name=Maverickb7)--][div class=\'quotetop\']QUOTE(Maverickb7 @ Apr 21 2006, 02:29 PM) [snapback]367279[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I used your code in place of mine and got the following error.

Parse error: parse error, unexpected T_IF in /home/public_html/functions.php on line 21

I renamed the function and don't know whats causing it.

Line 21 is

[code]if(strlen($text) <= $len){[/code]
[/quote]

sorry i have missed ';'
function limiter($text,$len) {
$ret = "";
$text = strip_tags($text)
if(strlen($text) <= $len){
$ret = $text;
} else {
$ret = strpos($text,0,$len);
$ret .= "...";
}
return $ret;
}
Link to comment
https://forums.phpfreaks.com/topic/8068-limit-text/#findComment-29418
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.